Gotchas in the C++ programing language

    技术2022-05-11  95

    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;}  

    最新回复(0)