C++構造函數和析構函數的調用順序


1、構造函數的調用順序 

基類構造函數、對象成員構造函數、派生類本身的構造函數  

2、析構函數的調用順序

派生類本身的析構函數、對象成員析構函數、基類析構函數(與構造順序正好相反) 

3、特例 

局部對象,在退出程序塊時析構

靜態對象,在定義所在文件結束時析構

全局對象,在程序結束時析構 

繼承對象,先析構派生類,再析構父類 

對象成員,先析構類對象,再析構對象成員

4、例子 

#include <iostream>
using namespace std;

class Base1
{
public:
    Base1(void){cnt++;cout<<"Base1::constructor("<<cnt<<")"<<endl;}
    ~Base1(void){cnt--;cout<<"Base1::deconstructor("<<cnt + 1<<")"<<endl;}
private:
    static int cnt;
};
int Base1::cnt = 0;

class Base2
{
public:
    Base2(int m){num = m; cout<<"Base2::constructor("<<num<<")"<<endl;}
    ~Base2(void){cout<<"Base2::deconstructor("<<num<<")"<<endl;}
private:
    int num;
};

class Example
{
public:
    Example(int n){num = n; cout<<"Example::constructor("<<num<<")"<<endl;}
    ~Example(void){cout<<"Example::deconstructor("<<num<<")"<<endl;}
private:
    int num;
};

class Derived:public Base1, public Base2
{
public:
    Derived(int m, int n):Base2(m),ex(n){cnt++;cout<<"Derived::constructor("<<cnt<<")"<<endl;}
    ~Derived(void){cnt--;cout<<"Derived::deconstructor("<<cnt+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) //不能輸出 

 

#include <iostream>
using 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
 */

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM