zoukankan      html  css  js  c++  java
  • 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
     */
    复制代码
  • 相关阅读:
    自由群(2)|完整版
    模1|同态
    交换代数笔记1|Atiyah,Chpt.1
    微分形式的几何第一章笔记
    矩阵群的一些简单习题1
    自由群1:基本概念
    代数的一些习题2|自由群与自由交换群
    蛤车1:两个习题,群作用与覆叠空间,N-S定理
    提升引理:唯一提升与同伦提升
    复变函数:复函数的空间与Montel定理
  • 原文地址:https://www.cnblogs.com/lidabo/p/9328323.html
Copyright © 2011-2022 走看看