1. 基类构造函数被调用的顺序以类派生表中声明的顺序为准.
class A
{
public:
A()
{
cout << "A::A() called" << endl;
num = 10;
}
void showMsg()
{
cout << "A::showMsg() called" << endl;
}
protected:
void showMsg2()
{
cout << "A::showMsg2() called" << endl;
}
private:
int num;
};
class B:private A
{
public:
B()
{
cout << "B::B() called" << endl;
}
void showMsgB()
{
cout << "B::showMsgB()" << endl;
}
void showMsgB2()
{
showMsg2();
}
};
class E
{
public:
E()
{
cout << "E::E()" << endl;
}
};
class F:public E, public B
{
public:
F()
{
cout << "F::F()" << endl;
}
};
...
int main(void)
{
F f;
return 0;
}
输出内容为:
E::E()
A::A() calledB::B() calledF::F()
2. 在含有基类、初始化成员函数的构造函数的调用顺序,基类最先调用->其次是成员初始化列表按照声明顺序进行调用->然后是成员类变量构造函数->本类的构造函数