考虑以下类定义:
class C {
public:
int i = 9;
const int j = 2;
};
在编译(使用C++11,例如g++ -o test test.cpp
)时,无需使用标志来启用g++,编译器就会抱怨成员变量初始化。但是,使用-std=c++11
可以很好地工作。
为什么这个规则在C++11中被改变了?是否认为以这种方式初始化成员变量的做法是错误的?
发布于 2014-09-10 19:39:33
在声明时初始化非静态数据成员不仅仅是“糟糕的实践”,在C++11之前根本不可能实现。
等效的C++03代码是:
class C
{
public:
C() : i(9), j(2)
{
}
int i;
const int j;
};
以防万一:What is this weird colon-member syntax in the constructor?
https://stackoverflow.com/questions/25773714
复制相似问题