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

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

2011-09-14 21:52 239 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: