#include <iostream>
using namespace std;
class Base0 // 基类Base0的声明
{
public:
Base0(int y) { x=y; cout<<"x of Base0: "<<x<<endl;}
int x;
};
class Base1 : virtual public Base0 // Base0为虚基类,公有派生Base1类
{
public:
Base1(int y):Base0(y) { cout<<"x of Base1: "<<x<<endl;}
};
class Base2 : virtual public Base0 // Base0为虚基类,公有派生Base2类
{
public:
Base2(int y):Base0(y) { cout<<"x of Base2: "<<x<<endl;}
};
class Child : public Base1, public Base2
{
public:
Child(int y,int u,int v,int w,int m,int n):Base0(y),Base1(y),Base2(y),B2(w),B0(u),B1(v),m(m),n(n){ cout<<"x m n of Child: "<<x<<" "<<m<<" "<<n<<endl;}
Base0 B0;
Base1 B1;
Base2 B2;
int m;
int n;
};
int main()
{
Child child(1,2,3,4,5,6);
return 0;
}
由结果可知构造对象child 的顺序为:
1.执行虚基类的构造函数
2.按继承列表次序执行非虚基类构造函数(不再执行虚基类的构造函数)
3.按定义顺序进行派生类新增成员对象初始化(若新增对象未出现在初始化列表中,执行默认构造函数)
4.基本数据类型初始化
5.执行函数体