临时对象相关

class B
{
public:
	B(){
		cout << "default constructor" << endl;
	}
	~B(){
		cout << "destructed" << endl;
	}
	B(int i) :data(i){
		cout << "constructed by parameter" << data << endl;
	}
	B(B& b)
	{
		this->data = b.data;
		cout << "this is copy constructor" << data << endl;
	}
	B& operator =(const B& b)
	{
		this->data = b.data;
		cout << "this is = operator" << data << endl;
		return *this;
	}
private:
	int data;
};
B play(B b)
{
	return b;
}
B fun()
{
	B b;
	return b;
}
int _tmain(int argc, _TCHAR* argv[])
{
int _tmain(int argc, _TCHAR* argv[])
{
	{
		B t1;
		t1 = play(2);
	}
	cout << "***********" << endl;
	{
		B t1 = play(2);
	}
	system("pause");
}
调用结束时候,赋值操作比对象定义时候初始化要多创建一次。个人理解可以认为是赋值两边都是对象;拷贝复制对象并不需要;

你可能感兴趣的:(临时对象相关)