对构造函数、拷贝构造函数和赋值操作符调用的简单回顾

本文只是测试构造函数、拷贝构造函数和赋值操作符的调用问题,不涉及这些函数内的具体实现,所以多包涵

还有如果觉得有不足之处还望指出,谢谢…………

 

 

#include<iostream>

using namespace std;

class Object{  
	
 	 char* name;
public:
	Object() 
	{
		cout << "Objected is constructed.(无参数)" << endl; 
	}

	Object(char* s) //
	{
		name = s;//
		cout << "Objected is constructed.(参数)" << endl; 
	}

	Object( const Object& o) 
	{
//为对象申请内存并进行出错检查;(本文没有处理)
//从对象o复制到本对象
		name = o.name;
		cout << "Object is copied." << endl; 
	}

	char* getName() 
	{ 
		return name;
	}
	Object & operator = (const Object& lala)
	{
		name = lala.name;
		cout << "Object is operator =." << endl; 

		return *this;
	}

	~Object()
	{ 
		cout << "Object is destructed." << endl;
	}


};

Object getObject( Object o)
{ 
	cout << "getO" << endl;//

	return o; 
}

Object getObject1( Object& o)
{ 
	cout << "getO1" << endl;//

	return o; 
}

Object& getObject2( Object& o)
{ 
	cout << "getO2" << endl;//

	return o; 
}


int main()
{

	Object o("zhangsan");

	Object o1;
	o1 = getObject( o);
	cout << endl;

	cout << "Object o2 = getObject(o):" << endl;
	Object o2 = getObject(o);//相当于Object o2(getObject(o));
	cout << endl;

	cout << "o1 = getObject(o):" << endl;
	o1 = getObject(o); 
	cout << endl << endl;

//getObject1
	cout << "getObject1:" << endl;
	Object o11;
	o11 = getObject1( o);
	cout << endl;

	cout << "Object o22 = getObject(o):" << endl;
	Object o22 = getObject1(o);
	cout << endl;

	cout << "o11 = getObject(o):" << endl;
	o11 = getObject(o);
	cout << endl << endl;

//getObject2
	cout << "getObject2:" << endl;
	Object o111;
	o111 = getObject2( o);
	cout << endl;

	cout << "Object o222 = getObject(o):" << endl;
	Object o222 = getObject2(o);
	cout << endl;

	cout << "o111 = getObject(o):" << endl;
	o111 = getObject(o);

	cout << "end" << endl;

	return 0;
}
 

 

 

Objected is constructed.(参数)

Objected is constructed.(无参数)

Object is copied.           //每次传参都会构建临时对象

getO

Object is copied.

Object is destructed.

Object is operator =.

Object is destructed.

 

Object o2 = getObject(o):

Object is copied.

getO

Object is copied.

Object is destructed.

 

o1 = getObject(o):

Object is copied.

getO

Object is copied.

Object is destructed.

Object is operator =.

Object is destructed.

 

 

getObject1:

Objected is constructed.(无参数)

getO1

Object is copied.

Object is operator =.

Object is destructed.

 

Object o22 = getObject(o):

getO1

Object is copied.

 

o11 = getObject(o):

Object is copied.

getO

Object is copied.

Object is destructed.

Object is operator =.

Object is destructed.

 

 

getObject2:

Objected is constructed.(无参数)

getO2

Object is operator =.

 

Object o222 = getObject(o):

getO2

Object is copied.

 

o111 = getObject(o):

Object is copied.

getO

Object is copied.

Object is destructed.

Object is operator =.

Object is destructed.

end


拷贝构造函数和赋值操作符:

同样是利用现有对象的值,生成/更新另一个对象的值。
区别在于:拷贝构造函数是去完成对未初始化的存储区的初始化,而赋值操作符则是处理一个已经存在的对象。对一个对象赋值,当它一次出现时,它将调用拷贝构造函数,以后每次出现,都调用赋值操作符。

定义对象a,并用另一个对象b对a进行初始化时,
若没有拷贝构造函数时,那么编译器会自动生成一个
T b(1);//假设对象b已经初始化
T a(b);//初始化阶段,调用拷贝构造函数
T c = b;//虽然使用了“=”,但是初始化阶段,仍调用拷贝构造函数
c = a; //因为对象c已经定义,所以此处调用赋值操作符重载函数。如果没有编译器会自动生成一个。 

 

你可能感兴趣的:(C++,构造函数调用,拷贝构造函数调用,赋值操作符调用)