在C++11中,您可以这样做:
class Foo
{
public:
Foo();
bool test = false;
};
Foo::Foo()
{
// is test guaranteed to be false now? and is it surely not overriden later?
this->test = true;
}
我想知道的基本上在注释中已经描述了,我可以通过我的编译器来验证,但这是否在标准中提到?它在所有平台和所有编译器上是否都是相同的?
Foo(): test(false) { this->test = true; }
一样。 - Adamtest
,而不是将其初始化为 false 然后再赋值为 true,那么可以像往常一样使用初始化列表:Foo(string s) : test{true} {}
,Foo(bool b) : test{b} {}
,等等。 - peppe