C++调用顺序

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.  在含有基类、初始化成员函数的构造函数的调用顺序,基类最先调用->其次是成员初始化列表按照声明顺序进行调用->然后是成员类变量构造函数->本类的构造函数


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值