1、构造函数的调用顺序
基类构造函数、对象成员构造函数、派生类本身的构造函数
2、析构函数的调用顺序
派生类本身的析构函数、对象成员析构函数、基类析构函数(与构造顺序正好相反)
3、特例
局部对象,在退出程序块时析构
静态对象,在定义所在文件结束时析构
全局对象,在程序结束时析构
继承对象,先析构派生类,再析构父类
对象成员,先析构类对象,再析构对象成员
4、例子
#includeusing namespace std; class base1 { public: base1(void){cnt ;cout<<"base1::constructor("< ")"<<endl;} ~base1(void){cnt--;cout<<"base1::deconstructor("< 1<<")"<<endl;} private: static int cnt; }; int base1::cnt = 0; class base2 { public: base2(int m){num = m; cout<<"base2::constructor("< ")"<<endl;} ~base2(void){cout<<"base2::deconstructor("< ")"<<endl;} private: int num; }; class example { public: example(int n){num = n; cout<<"example::constructor("< ")"<<endl;} ~example(void){cout<<"example::deconstructor("< ")"<<endl;} private: int num; }; class derived:public base1, public base2 { public: derived(int m, int n):base2(m),ex(n){cnt ;cout<<"derived::constructor("< ")"<<endl;} ~derived(void){cnt--;cout<<"derived::deconstructor("< 1<<")"<<endl;} private: example ex; static example stex; //example::constructor(1) //不能输出 static int cnt; }; int derived::cnt = 0; derived ge_a(1,2); // base1::constructor(1) // base2::constructor(1) // example::constructor(2) // derived::constructor(1) static derived gs_b(3,4); // base1::constructor(2) // base2::constructor(3) // example::constructor(4) // derived::constructor(2) int main(void) { cout << "---------start---------" << endl; derived d(5,6); // base1::constructor(3) // base2::constructor(5) // example::constructor(6) // derived::constructor(3) derived e(7,8); // base1::constructor(4) // base2::constructor(7) // example::constructor(8) // derived::constructor(4) cout << "----------end----------" << endl; //derived e(7,8) 析构 // derived::deconstructor(4) // example::deconstructor(8) // base2::deconstructor(7) // base1::deconstructor(4) //derived d(5,6) 析构 // derived::deconstructor(3) // example::deconstructor(6) // base2::deconstructor(5) // base1::deconstructor(3) return 0; } //static derived gs_b(3,4) 析构 // derived::deconstructor(2) // example::deconstructor(4) // base2::deconstructor(3) // base1::deconstructor(2) //derived ge_a(1,2) 析构 // derived::deconstructor(1) // example::deconstructor(2) // base2::deconstructor(1) // base1::deconstructor(1) //static example stex 析构 //example::deconstructor(1) //不能输出
#includeusing namespace std; class a { public: a(){cout<<"a::constructor"<<endl;}; ~a(){cout<<"a::deconstructor"<<endl;}; }; class b { public: b(){cout<<"b::constructor"<<endl;}; ~b(){cout<<"b::deconstructor"<<endl;}; }; class c : public a { public: c(){cout<<"c::constructor"<<endl;}; ~c(){cout<<"c::deconstructor"<<endl;}; private: // static b b; b b; }; class d : public c { public: d(){cout<<"d::constructor"<<endl;}; ~d(){cout<<"d::deconstructor"<<endl;}; }; int main(void) { c* pd = new d(); delete pd; return 0; } /* output ----->b b a::constructor b::constructor c::constructor d::constructor c::deconstructor b::deconstructor a::deconstructor ----->static b b a::constructor c::constructor d::constructor c::deconstructor a::deconstructor */