ase2
{
public:
? ? Base2(int i)
? ? {
? ? ? ? cout << "Base2 " << i << endl;
? ? }
};
?
class Base3
{
public:
? ? Base3()
? ? {
? ? ? ? cout << "Base3 *" << endl;
? ? }
};
?
class Derived : public Base2, ?public Base1, virtual public Base3
{
public:
? ? Derived(int a, int b, int c, int d, int e)
? ? ? ? : Base1(a), b2(d), b1(c), Base2(b)
? ? {
? ? ? ? m = e;
? ? ? ? cout << "Derived.\n";
? ? }
protected:
? ? Base1 b1;
? ? Base2 b2;
? ? Base3 b3;
? ? int m;
};
?
void test()
{
? ? Derived aa(1, 2, 3, 4, 5);
? ? cout << "This is ok.\n";
}
?
int main()
{
? ? test();
? ? return 0;
}
/*
Base3 *
Base2 2
Base1 1
Base1 3
Base2 4
Base3 *
Derived.
This is ok.
*/
分析:
?
(1) virtual?
?
按照继承顺序:Base3
?
第一步:先继承Base3,在初始化列表里找不到Base3(), 则调用Base3里的默认构造函数Base3(),打印"Base3 ?*"
?
(2)non-virtual
?
按照继承顺序:Base2,Base1
?
第二步:继承Base2,在初始化列表中找Base2(b),调用Base2的构造函数Base2(2),打印"Base2 2"
?
第三步:继承Base1,在初始化列表中找Base1(a),调用Base1的构造函数Base1(1),打印"Base1 1"
?
?(3)data member
?
按照申明顺序:b1,b2,b3
?
第四步:构造b1,在初始化列表中找b1(c),调用Base1的构造函数Base1(3),打印"Base1 3"
?
第五步:构造b2,在初始化列表中找b2(d),调用Base2的构造函数Base1(4),打印"Base2 4"
?
第六步:构造b3,在初始化列表中找不到b3(),调用Base3的构造函数Base3(),打印"Base3 *"
?
(4)self
?
第7步:执行自己的构造函数体,输出"Derived."