返回对象时,我不能简单地在类名后使用 {}
When returning an object, I can't simply use {} after class name
以有关嵌套类的教育代码为例:
class enclose {
struct nested { // private member
void g() {}
};
public:
static nested f() { return nested{}; }
};
int main() {
//enclose::nested n1 = e.f(); // error: 'nested' is private
enclose::f().g(); // OK: does not name 'nested'
auto n2 = enclose::f(); // OK: does not name 'nested'
n2.g(); }
将此代码复制并粘贴到Microsoft Visual Studio 2012时,我会在Line中出现错误
static nested f() { return nested{}; }
问题与函数返回嵌套的方式有关。这不是我第一次看到代码以这种方式返回值,但是我通常会忽略它,并以更长的方式进行。这是编译器问题吗?
行
return nested{};
采用新的C 11支撑启动化,并将对象进行价值化。如您在此处看到的那样,Visual Studio 2012(VC11)不支持Braced Initialization,因此您会遇到编译时间错误。
唯一的解决方案是使用
return nested();
而不是更新编译器。
相关文章:
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法
- 类的方法和对象。参考?智能指针?简单的初始化?
- 有没有一种简单的方法可以在对象向量上调用构造函数?
- "std::function"的简单版本:函数对象的生存期?
- 面向对象架构,用于简单的基于着色器的GL程序
- 是否有一种简单的方法可以在运行时在C 中创建/名称对象
- 是否可以简单地序列化C++对象
- 返回对象时,我不能简单地在类名后使用 {}
- 创建返回对象引用的成员函数的最简单方法
- 2组并集;返回简单对象-C++
- 使用本机/C++代码在VS2010中创建简单COM对象的最简单方法是什么
- c++:旋转锁或互斥对象比较(简单计算)
- 比较来自同一类型的两个对象的数据成员的最简单方法
- 为什么简单析构函数在使用基指针声明时不删除派生对象
- 序列化/封送C#中的简单对象以通过网络发送,供非托管C++应用程序读取
- 在将对象移动到另一个线程后,不能简单地删除该对象
- 简单的引用变量赋值导致对象的全局指针出现segfault
- 将对象引用存储在一个简单的容器中
- OpenCV C++简单的对象跟踪
- 非常简单的程序通过了VS2010 c++内存泄漏检查器,但在销毁所有对象后仍然在程序端使用更多内存