首页 > 代码库 > 多继承的构造顺序

多继承的构造顺序

构造函数按一下顺序被调用:

1.按虚拟基类被继承的顺序

2.按非虚拟基类被继承的顺序

3.按成员函数被声明的顺序

4.类自己的构造函数

 

#include<stdio.h>#include<iostream>#include<assert.h>using namespace std;class OBJ1{public:    OBJ1(){ cout<<"OBJ1"<<endl; }};class OBJ2{public:    OBJ2(){ cout<<"OBJ2"<<endl; }};class Base1{public:    Base1(){ cout<<"Base1"<<endl; }};class Base2:public Base1{public:    Base2(){ cout<<"Base2"<<endl; }};class Base3{public:    Base3(){ cout<<"Base3"<<endl; }};class Base4:public Base3,public Base1{public:    Base4(){ cout<<"Base4"<<endl; }};class Test:public Base1,public virtual Base2,public virtual Base3,public Base4{public:    Test():Base4(),Base3(),Base2(),Base1(),obj2(),obj1()    { cout<<"Test"<<endl; }private:    OBJ2 obj2;    OBJ1 obj1;};int main(){    Test t;}

多继承的构造顺序