首页 > 编程语言 > 详细

C++继承中的成员初始化顺序

时间:2020-03-14 18:30:29      阅读:64      评论:0      收藏:0      [点我收藏+]
#include <iostream>
using namespace std;

class Base1 {
public:
    Base1(int i) { cout << "Constructing Base1 " << i << endl; }
    ~Base1() { cout << "Destructing Base1" << endl; }
};

class Base2 {
public:
    Base2(int j) { cout << "Constructing Base2 " << j << endl; }
    ~Base2() { cout << "Destructing Base2" << endl; }
};

class Base3 {
public:
    Base3() { cout << "Constructing Base3 *" << endl; }
    ~Base3() { cout << "Destructing Base3" << endl; }
};

class Derived: public Base2, public Base1, public Base3 {

public:
    Derived(int a, int b, int c, int d): Base1(a), member2(d), member1(c), Base2(b) { }
private:
    Base3 member3;
    Base1 member1;
    Base2 member2;
    
};

int main() {
    Derived obj(1, 2, 3, 4);
    return 0;
}

 

C++继承中的成员初始化顺序

原文:https://www.cnblogs.com/lyt888/p/12493411.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!