-
单继承中的构造顺序问题
-
先构造父类的再构造子类的,析构顺序相反
-
-
多继承中构造顺序问题
-
任何构造顺序问题都和初始化参数列表无关
-
构造顺序和继承顺序一致
-
单继承
class A
{
public:
A() { cout << "A"; }
~A() { cout << "A" ; }
protected:
};
class B :public A
{
public:
B() { cout << "B"; }
~B() { cout << "B" ; }
};
int main()
{
B b; //构造一个对象
return 0;
}
/*输入*/
ABBA
多继承
class A
{
public:
A() { cout << "A"; }
~A() { cout << "A" ; }
protected:
};
class B :public A
{
public:
B() { cout << "B"; }
~B() { cout << "B" ; }
};
class C
{
public:
C() { cout << "C"; }
~C() { cout << "C"; }
};
class D
{
public:
D() { cout << "D"; }
~D() {cout << "D";}
};
//ACD类产生F类 构造顺序和继承顺序一致:先继承C,再继承A,最后继承D
class F :public C, public A, public D
{
public:
//任何构造顺序问题,都和初始化参数列表无关
F() { cout << "F";}
~F() { cout << "F";}
};
//CADF FDAC
int main()
{
F f; //构建一个对象
return 0;
}
/*输出*/
CADFFDAC