您的位置:首页 > 编程语言 > C语言/C++

C++调用顺序

2014-01-02 15:25 211 查看
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.  在含有基类、初始化成员函数的构造函数的调用顺序,基类最先调用->其次是成员初始化列表按照声明顺序进行调用->然后是成员类变量构造函数->本类的构造函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: