构造和析构顺序
#include<iostream>
using namespace std;class Base
{
public:Base(){cout << "Base构造函数" << endl;}~Base(){cout << "Base析构函数" << endl;}
};class Son :public Base
{
public:Son(){cout << "Son构造函数" << endl;}~Son(){cout << "Son析构函数" << endl;}
};
void test01()
{Son s;
}
int main() {test01();return 0;
}
结论:
继承中先调用父类构造函数,再调用子类构造函数,析构顺序与构造顺序相反