gpt4 book ai didi

c++ - 为什么基类的构造函数先调用?

转载 作者:行者123 更新时间:2023-11-27 22:44:13 24 4
gpt4 key购买 nike

<分区>

运行下面的代码,为什么先声明一个派生类的对象,还是先派生基类的构造函数

#include<iostream>

using namespace std;

class base {
public:
base()
{ cout<<"Constructing base \n"; }
~base()
{ cout<<"Destructing base \n"; }
};

class derived: public base {
public:
derived()
{ cout<<"Constructing derived \n"; }
~derived()
{ cout<<"Destructing derived \n"; }
};

int main(void)
{
derived *d = new derived(); //d is defined ahead of the base class object
base *b = d;
delete b;

return 0;
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com