C++对象初始化(构造函数、拷贝构造函数、移动拷贝构造函数)

1 行为

初始化时直接方式(小括号方式):调用拷贝构造函数;
初始化时赋值方式:调用拷贝构造函数;
初始化后赋值方式:调用赋值运算符重载函数;
直接初始化列表方式(大括号方式传入类型对象):调用拷贝构造函数;
赋值初始化列表方式(等号后大括号方式类型对象):调用拷贝构造函数;
直接初始化列表方式(大括号方式传入成员列表):调用匹配的有参构造函数;
赋值初始化列表方式(等号后大括号方式传入成员列表):调用匹配的有参构造函数;

初始化直接方式结合移动语义:调用移动拷贝构造函数
初始化时赋值方式结合移动语义:调用移动拷贝构造函数
初始化后赋值方式结合移动语义:调用赋值运算符重载函数;

2 示例代码

#include <iostream>
#include <string>
using namespace std;
class A
{
private:
	string name;
	int value;
public:
	A()
	{
		value = 1;
		name = "aloha";
		cout << "call A() this="<<this << endl;
	}
	A(int value)
	{
		this->value = value;
		cout << "call A(int )" << endl;
	}
	A(string name,int value)
	{
		this->name = name;
		this->value = value;
		cout << "call A(string,int)" << endl;
	}
	 
	A( const A& a)
	{
		cout << "call A(A&) this=" << this << endl;
		a.show();
		this->name = a.name;
		this->value = a.value;
	}
	A& operator=(const A& a)
	{

		cout << "call =A(A&) this=" << this << endl;
		a.show();
		this->name = a.name;
		this->value = a.value;
		 
		return *this;
	}

	A(const A&& a)
	{
		cout << "call A(A&&) this=" << this << endl;
		a.show();
		this->name = a.name;
		this->value = a.value;
	}
	void show()const
	{
		cout << "object addr=" << this << ",name=" << name << ",value=" << value<<endl;
	}
}; 
 

int main()
{ 
	A a;
	a.show();

	cout << endl << "case 1:" << endl;
	A a1(a);
	a1.show();

	cout << endl << "case 2:" << endl;
	A a2 = a;
	a2.show();

	cout << endl << "case 3:" << endl;
	A a3;
	a3=a;
	a3.show();

	cout << endl << "case 4:" << endl;
	A a4(move(a)); 
	a4.show();

	cout << endl << "case 5:" << endl;
	A a5 = move(a) ;
	a5.show();

	cout << endl << "case 6:" << endl;
	A a6 ;
	a6 = move(a);
	a6.show();

	cout << endl << "case 7:" << endl;
	A a7{a}; 
	a7.show();

	cout << endl << "case 8:" << endl;
	A a8 = { a };
	a8.show();

	cout << endl << "case 9:" << endl;
	A a9 = { "aloha",5 };
	a9.show();

	cout << endl << "case 10:" << endl;
	A a10 { "aloha",5 };
	a10.show();
}

3 运行情况

call A() this=010FFD78
object addr=010FFD78,name=aloha,value=1

case 1:
call A(A&) this=010FFD50
object addr=010FFD78,name=aloha,value=1
object addr=010FFD50,name=aloha,value=1

case 2:
call A(A&) this=010FFD28
object addr=010FFD78,name=aloha,value=1
object addr=010FFD28,name=aloha,value=1

case 3:
call A() this=010FFD00
call =A(A&) this=010FFD00
object addr=010FFD78,name=aloha,value=1
object addr=010FFD00,name=aloha,value=1

case 4:
call A(A&&) this=010FFCD8
object addr=010FFD78,name=aloha,value=1
object addr=010FFCD8,name=aloha,value=1

case 5:
call A(A&&) this=010FFCB0
object addr=010FFD78,name=aloha,value=1
object addr=010FFCB0,name=aloha,value=1

case 6:
call A() this=010FFC88
call =A(A&) this=010FFC88
object addr=010FFD78,name=aloha,value=1
object addr=010FFC88,name=aloha,value=1

case 7:
call A(A&) this=010FFC60
object addr=010FFD78,name=aloha,value=1
object addr=010FFC60,name=aloha,value=1

case 8:
call A(A&) this=010FFC38
object addr=010FFD78,name=aloha,value=1
object addr=010FFC38,name=aloha,value=1

case 9:
call A(string,int)
object addr=010FFC10,name=aloha,value=5

case 10:
call A(string,int)
object addr=010FFBE8,name=aloha,value=5

备注:g++编译时添加c++11支持,-std=c++11

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值