C++ 构造函数初始化顺序

构造函数初始化按照如下顺序进行

1. 首先构造虚拟基类,任何虚拟基类的构造函数按照它们被继承的顺序构造;

2. 其次构造非虚拟基类,任何非虚拟基类的构造函数按照它们被继承的顺序构造;

3. 接着构造成员对象,任何成员对象的构造函数按照它们声明的顺序调用;

4. 最后调用类自身的构造函数;

析构函数就无脑的将构造函数顺序反转即可。


接下来举一个简单但是全面的例子帮助理解

#include <iostream>

using namespace std;

class OBJ1
{
public:
	OBJ1() { cout << "OBJ1" << endl; }
	~OBJ1() {	cout << "OBJ1 destory" << endl;}
};

class OBJ2
{
public:
	OBJ2() { cout << "OBJ2\n"; }
	~OBJ2(){cout << "OBJ2 destory" <<endl;}
};

class Base1
{
public:
	Base1() { cout << "Base1" << endl; }
	~Base1() { cout << "Base1 destory" << endl; }
};

class Base2
{
public:
	Base2() { cout << "Base2" << endl; }
	~Base2() { cout << "Base2 destory" << endl; }
};

class Base3
{
public:
	Base3() { cout << "Base3" << endl; }
	~Base3() { cout << "Base3 destory" << endl; }
};

class Base4
{
public:
	Base4() { cout << "Base4" << endl; }
	~Base4() { cout << "Base4 destory" << endl; }
};

class Derived :public Base1, virtual public Base2,
	public Base3, virtual public Base4
{
public:
	Derived() { cout << "Derived ok" << endl; }
	~Derived() { cout << "Derived destory" << endl; }
protected:
	OBJ1 obj1;
	OBJ2 obj2;
};

int main()
{
	Derived aa;
	cout << "construct ok"<<endl;
	return 0;
}

输出结果如下

Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived
----------------------
construct ok
----------------------
Derived destory
OBJ2 destory
OBJ1 destory
Base3 destory
Base1 destory
Base4 destory
Base2 destory

在程序中,Derived继承于四个基类,其中Base2和Base4是虚拟基类,且Base2在Base4之前被继承,所以按照顺序Base2先构造。

虚拟基类构造完毕,构造非虚拟基类Base1和Base3,同理按照顺序构造,Base1在Base3之前。

基类构造完毕,构造成员对象obj1和obj2,同理按照顺序。最后调用类自身构造函数,输出Derived。

析构函数顺序完全相反,无需解释。


当然,我们也会遇到多重继承的问题,那么先调用基类的构造函数,如果基类的构造函数中仍然存在基类,那么程序会继续进行向上查找,直到找到它最早的基类进行初始化。

说起来可能比较难懂,同样的我们举个栗子。

#include<iostream>
using namespace std;
class Base
{
public:
	Base() { std::cout << "Base::Base()" << std::endl; }
	~Base() { std::cout << "Base::~Base()" << std::endl; }
};

class Base1 :public Base
{
public:
	Base1() { std::cout << "Base1::Base1()" << std::endl; }
	~Base1() { std::cout << "Base1::~Base1()" << std::endl; }
};

class Derive
{
public:
	Derive() { std::cout << "Derive::Derive()" << std::endl; }
	~Derive() { std::cout << "Derive::~Derive()" << std::endl; }
};

class Derive1 :public Base1
{
private:
	Derive m_derive;
public:
	Derive1() { std::cout << "Derive1::Derive1()" << std::endl; }
	~Derive1() { std::cout << "Derive1::~Derive1()" << std::endl; }
};

int main()
{
	Derive1 derive;
	return 0;
}


输出结果如下

Base::Base()
Base1::Base1()
Derive::Derive()
Derive1::Derive1()
Derive1::~Derive1()
Derive::~Derive()
Base1::~Base1()
Base::~Base()

在该例中,Derivel1 继承自Base1,Base1继承自Base,所以按照顺序构造则是Base,Base1,然后构造Derive1。而Derive1有一个成员对象Derive,所以按照顺序在调用Derive1的构造函数前,需要构造Derive(见开头第3点)。

通过以上两个例子,相信你一定对C++构造函数初始化顺序有了一定的了解。(滑稽)




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值