一、结论
1. 非静态成员按照声明的顺序初始化。
2. 尽量在声明的地方初始化。
3. 尽量避免在构造函数的代码块做初始化。
二、代码实例
#include <iostream>
#include <string>
using namespace std;
struct Object {
static int seq;
Object() {
cout << "defualt construct Object:" << seq++
<< "(" << this << ")" << endl;
}
Object(int a) {
cout << "converstion construct Object " << seq++
<< "(" << this << ")" << endl;
}
Object& operator=(const Object& other) {
cout << "copy construct Object " << seq++
<< "(" << this << ")" << endl;
return *this;
}
};
int Object::seq = 0;
struct User {
Object obj1 = 0;