【发布时间】:2011-07-25 21:11:26
【问题描述】:
在选项 1 和选项 2 中初始化变量的优缺点是什么?
class MyClass
{
public:
MyClass( float f, char a );
private:
float mFloat;
char mCharacter;
bool mBoolean;
int mInteger;
};
MyClass::MyClass( float f, char a ) : mFloat( f ), mBoolean( true ) // option 1.
{
// option 2
mCharacter = a;
mInteger = 0;
}
编辑: 为什么选项 2 如此普遍?
【问题讨论】:
-
不再与添加的编辑重复。由于选项 1 显然更好,我很好奇为什么选项 2 如此普遍。
-
它可能是重复的,但似乎其他问题的答案是详尽的。
标签: c++ constructor initialization