zoukankan      html  css  js  c++  java
  • 派生类构造对象时,构造函数执行顺序

    先调用基类构造函数,再调用派生类构造函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18

    class Base
    {
    public:
        Base() { cout << "Base()" << endl;}
    };
     
     
    class Derived : public Base
    {
    public:
        Derived() { cout << "Derived()" << endl;}
    };
     
    int main()
    {
        Derived d;
        return 0;
    }

    输出:

    Base()
    Derived()

     

    先调用基类构造函数,再调用对象成员的构造函数,最后调用派生类构造函数.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19

    class Base
    {
    public:
        Base() { cout << "Base()" << endl;}
    };
     
     
    class Derived : public Base
    {
    public:
        Base b1, b2;
        Derived() { cout << "Derived()" << endl;}
    };
     
    int main()
    {
        Derived d;
        return 0;
    }

    输出:

    Base()
    Base()
    Base()
    Derived()

    先调用基类构造函数,再调用对象成员的构造函数(对象声明顺序,不是继承顺序,更不是初始化成员列表顺序),最后调用派生类构造函数.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    class Base1
    {
    public:
        Base1() { cout << "Base1()" << endl;}
    };
     
    class Base2
    {
    public:
        Base2() { cout << "Base2()" << endl;}
    };
     
    class Derived : public Base1, public Base2
    {
    public:
        Base1 b1;
        Base2 b2;
        Derived() { cout << "Derived()" << endl;}
    };
     
    int main()
    {
        Derived d;
        return 0;
    }

    输出:

    Base1()
    Base2()
    Base1()
    Base2()
    Derived()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    class Base1
    {
    public:
        Base1() { cout << "Base1()" << endl;}
    };
     
    class Base2
    {
    public:
        Base2() { cout << "Base2()" << endl;}
    };
     
    class Derived : public Base1, public Base2
    {
    public:
        Base2 b2;
        Base1 b1;
        Derived() { cout << "Derived()" << endl;}
    };
     
    int main()
    {
        Derived d;
        return 0;
    }

    输出:

    Base1()
    Base2()
    Base2()
    Base1()
    Derived()

    先调用虚基类构造函数,再调用其他基类构造函数,然后调用对象成员构造,最后调用派生类构造函数.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    class Base1
    {
    public:
        Base1() { cout << "Base1()" << endl;}
    };
     
    class Base2
    {
    public:
        Base2() { cout << "Base2()" << endl;}
    };
     
    class Derived : public Base1,virtual public Base2
    {
    public:
        Base2 b2;
        Base1 b1;
        Derived() { cout << "Derived()" << endl;}
    };
     
    int main()
    {
        Derived d;
        return 0;
    }

    Base2()
    Base1()
    Base2()
    Base1()
    Derived()

    总结:先基类,再派生类;先虚基类,再其他基类;先对象成员,再派生类;顺序是声明顺序,而不是成员初始化列表顺序。

  • 相关阅读:
    github上十二款最著名的Android播放器开源项目
    ReactiveX/RxJava文档中文版
    腾讯开源的Android UI框架——QMUI Android
    android EventBus的简单使用
    android EventBus的简单使用
    MVP实战心得—封装Retrofit2.0+RxAndroid+RxBus
    动态合并Repeater控件数据列
    动态合并GridView数据行DataRow的列
    找出1至10范围奇数
    判断某元素是否在Array中
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3208718.html
Copyright © 2011-2022 走看看