构造时:基类构造函数→对象成员构造函数→派生类本身的构造函数
析构时:派生类本身的析构函数→对象成员析构函数→基类析构函数
写个小程序验证一下:
#include <iostream>
using namespace std;
class component
{
public:
component() {cout << "construct component" << endl;};
~component() {cout<< "destruct component" << endl;};
};
class base
{
public:
base() {cout << "construct base" << endl;};
~base() {cout << "destruct base" << endl;};
};
class derived
:base
{
public:
derived() {cout << "construct derived" << endl;};
~derived() {cout << "destruct derived" << endl;};
component c;
};
int main()
{
derived d;
return 0;
}
输出是:
construct base
construct component
construct derived
destruct derived
destruct component
destruct base