对于派生类的构造函数,创建对象时构造函数执行的顺序

1)基类的构造函数。

2)成员对象构造函数。

3)派生类本身的构造函数。

记忆方式:先父母,再客人,后自己。

cpp 复制代码
#include <iostream>
#include <thread>
#include <string>
#include <iostream>
using namespace std;

class AAA {
public:
	AAA() { cout<<"AAA"<<endl;}
	~AAA(){} 
};
class CCC {
public:
	CCC() { cout<<"CCC"<<endl;}
	~CCC() {}
};

class BBB :public AAA{
public:
	BBB() { cout<<"BBB"<<endl;}
	~BBB() {}
private:
	CCC c;
};

int main()						
{
	BBB *b = new BBB;
	delete b;

	return 0;
}
相关推荐
_wyt0015 小时前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾8 小时前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you9 小时前
constexpr函数
c++
凡人叶枫10 小时前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫10 小时前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss10 小时前
BRpc使用
c++·rpc
-森屿安年-11 小时前
63. 不同路径 II
c++·算法·动态规划
chase_my_dream11 小时前
Cartographer详细讲解
c++·人工智能·自动驾驶
森G11 小时前
75、服务器源码解析---------云视频服务项目
linux·服务器·网络·c++·qt
碧海蓝天202211 小时前
C++法则24:在标准 C++ 中,没有任何可移植的方式判断指针 T* pt 指向的内存位置是否已经 构造了对象,程序员必须手动跟踪哪些元素已构造。
java·开发语言·c++