您的位置:首页 > 编程语言 > C语言/C++

Gotchas in the C++ programing language

2007-01-29 20:47 429 查看

Initializer lists

In C++, it is the order of the class inheritance and of the member variables that determine the initialization order, not the order of an initializer list:






#include <iostream>




class CSomeClass






{


public:


CSomeClass(int n)






{


std::cout << "CSomeClass constructor with value ";


std::cout << n << std::endl;


}


};




class CSomeOtherClass






{


public:


CSomeOtherClass() //In this example, despite the list order,


: obj2(2), obj1(1) //obj1 will be initialized before obj2.






{


//Do nothing.


}


private:


CSomeClass obj1;


CSomeClass obj2;


};




int main(void)






{


CSomeOtherClass obj;


return 0;


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: