c++利用初始化列表在类内部和类外部定义构造函数的区别

case 1:在类外定义构造函数,所有data member都在初始化列表中进行初始化。

class SupportClass
{
public:
	SupportClass()
	{
		cout << "SupportClass() is called" << endl;
	}
	SupportClass(SupportClass&)
	{
		cout << "SupportClass(SupportClass&) is called" << endl;
	}
	~SupportClass()
	{
		cout << "~SupportClass() is called" << endl;
	}
	SupportClass& operator=(SupportClass&)
	{
		cout << "SupportClass()& operator=(SupportClass&) is called" << endl;
		return *this;
	}
};

class TestInitialize
{
public:
	TestInitialize(SupportClass arg);
private:
	SupportClass data;
};

TestInitialize::TestInitialize(SupportClass arg) :data(arg){}

int main()
{
	SupportClass sc;
	cout << endl;
	TestInitialize ti(sc);
	return 0;
}

  

结果:

 第一次默认构造:来自main函数中定义的变量sc;

第二次拷贝构造:来自形参拷贝实参;

第三次拷贝构造:来自data member 拷贝形参。

结论:与类内定义一样。

case 2:在类外定义构造函数,初始化列表为空,data member 在函数体中定义。

TestInitialize::TestInitialize(SupportClass arg) { data = arg; }

  结果;

 第一次默认构造:来自main函数中定义的变量sc;

第二次拷贝构造:来自形参拷贝实参;

第三次默认构造:来自data member 的默认初始化。

接着的 operator=:data 复制 形参。

结论:与类内定义一样。

case 3:类有多个 data 成员,全用初始化列表初始化,函数体为空

添加一个class:AnotherClass

class AnotherSupportClass
{
public:
	AnotherSupportClass()
	{
		cout << "AnotherSupportClass() is called" << endl;
	}
	AnotherSupportClass(AnotherSupportClass&)
	{
		cout << "AnotherSupportClass(AnotherSupportClass&) is called" << endl;
	}
	~AnotherSupportClass()
	{
		cout << "~AnotherSupportClass() is called" << endl;
	}
	AnotherSupportClass& operator=(AnotherSupportClass&)
	{
		cout << "AnotherSupportClass()& operator=(AnotherSupportClass&) is called" << endl;
		return *this;
	}
};
TestInitialize::TestInitialize(SupportClass arg1, AnotherSupportClass arg2)
:data(arg1), anotherData(arg2){}

 结果:

意外发现: 

我的构造函数的参数列表是:先SupportClass,然后再AnotherSupportClass。

结果在由实参拷贝到形参时时:先AnotherSupportClass,再SupportClass。

又做了一个实验:再添加一个成员

class TestInitialize
{
public:
	TestInitialize(SupportClass arg1, AnotherSupportClass arg2,OtherSupportClass arg3);
private:
	SupportClass data;
	AnotherSupportClass anotherData;
	OtherSupportClass otherData;
};

TestInitialize::TestInitialize(SupportClass arg1, AnotherSupportClass arg2, OtherSupportClass arg3)
:data(arg1), anotherData(arg2),otherData(arg3){};

 依旧是逆序拷贝构造的,重点是我不信邪,循环了100次,都是这个结果。

结论:函数参数列表的拷贝是随机的,并不是按照参数出现的先后顺序。

case 4:函数只能被定义一次,要么在函数内,要么在函数外。使用初始化列表初始化时必须定义函数!

猜你喜欢

转载自www.cnblogs.com/XiaoXiaoShuai-/p/11498823.html
今日推荐