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

文章目录

  • 1 行为
  • 2 示例代码
  • 3 运行情况

1 行为

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

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

2 示例代码

#include 
#include 
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

你可能感兴趣的:(C/C++)