class BASE {
public:
BASE() { cout << "create BASE" << endl; }
~BASE(){ cout << "delete BASE" << endl; }
};
class BaseClient {
public:
BaseClient() { cout << "create BaseClient" << endl; }
~BaseClient() { cout << "delete BaseClient" << endl; }
};
class Base:public BASE {
public:
Base() { cout << "create Base" << endl; }
~Base() { cout << "delete Base" << endl; }
private:
BaseClient bc;
};
class DerivedClient {
public:
DerivedClient() { cout << "create DerivedClient" << endl; }
~DerivedClient() { cout << "delete DerivedClient" << endl; }
};
class Derived:public Base {
public:
Derived() { cout << "create Derived" << endl; }
~Derived() { cout << "delete Derived" << endl; }
private:
DerivedClient dc;
};
Derived d;
构造顺序:BASE -> BaseClient -> Base -> DerivedClient ->Derived
父类成员->父类->子类成员->子类
析构顺序:Derived -> DerivedClient -> Base ->BaseClient ->BASE
子类->子类成员->父类->父类成员
1、真正对成员变量初始化的位置是初始化列表,构造函数内是赋值
2、类中包含引用成员变量、const成员变量、自定义类型成员变量,必须在初始化列表位置进行初始化
3、若用户没有显示写出初始化列表,编译器会自动补全