C++对象模型 ch2 构造函数语意学

1.  Programmers new to C++ often have two common misunderstandings:

That a default constructor is synthesized for every class that does not define one

That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class

As you have seen, neither of these is true.

2.  This apparent anomaly between initialization order and order within the initialization list can lead to the following nasty pitfall

>> cat B.CPP
#include <iostream>
using namespace std;
class B
{
        public:
                int i;
                int j;
                B(int v):j(v),i(j){}
};

int main()
{
        B b(3);
        cout << b.i << endl;
}

运行结果:

>> ./a.out
-4197020

 

 

你可能感兴趣的:(C++对象模型 ch2 构造函数语意学)