拷贝构造函数应用场景之二

#include <iostream>

using namespace std;

class Test
{
public:
	Test()
	{
		cout << "test()..." << endl;
		m_x = 0;
		m_y = 0;
	}
	Test(int x, int y)
	{
		cout << "Test(int x, int y)..." << endl;

		m_x = x;
		m_y = y;
	}
	Test(const Test & another)
	{
		cout << "Test(const Test &)..." << endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void printT() {
		cout << "x = " << m_x << ", m_y = " << m_y << endl;
	}

	~Test() {
		cout << "~Test()..." << endl;
	}
private:
	int m_x;
	int m_y;
};


//场景4
Test func2()
{
	cout << "func2 begin..." << endl;
	Test temp(10, 20);
	temp.printT();

	cout << "func2 end..." << endl;

	return temp;
}//匿名的对象 = temp  匿名对象.拷贝构造(temp)

void test4()
{
	cout << "test4 being.. " << endl;
	func2();// 返回一个匿名对象。 当一个函数返回一个匿名对象的时候,函数外部没有任何
			//变量去接收它, 这个匿名对象将不会再被使用,(找不到), 编译会直接将个这个匿名对象
			//回收掉,而不是等待整改函数执行完毕再回收.
	//匿名对象就被回收。
	
	cout << "test4 end" << endl;
}


int main(void)
{
	test4();

	return 0;
}

运行结果!

#include <iostream>

using namespace std;

class Test
{
public:
	Test()
	{
		cout << "test()..." << endl;
		m_x = 0;
		m_y = 0;
	}
	Test(int x, int y)
	{
		cout << "Test(int x, int y)..." << endl;

		m_x = x;
		m_y = y;
	}
	Test(const Test & another)
	{
		cout << "Test(const Test &)..." << endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void printT() {
		cout << "x = " << m_x << ", m_y = " << m_y << endl;
	}

	~Test() {
		cout << "~Test()..." << endl;
	}
private:
	int m_x;
	int m_y;
};


Test func2()
{
	cout << "func2 begin..." << endl;
	Test temp(10, 20);
	temp.printT();

	cout << "func2 end..." << endl;

	return temp;
}//匿名的对象 = temp  匿名对象.拷贝构造(temp)


void test5()
{
	cout << "test 5begin.. " << endl;
	Test t1 = func2(); //会不会触发t1拷贝构造来   t1.拷贝(匿名)?
						//并不会触发t1拷贝,而是 将匿名对象转正 t1,
						//把这个匿名对象 起了名字就叫t1.

	cout << "test 5 end.." << endl;
}


int main(void)
{
	test5();

	return 0;
}

运行结果!

#include <iostream>

using namespace std;

class Test
{
public:
	Test()
	{
		cout << "test()..." << endl;
		m_x = 0;
		m_y = 0;
	}
	Test(int x, int y)
	{
		cout << "Test(int x, int y)..." << endl;

		m_x = x;
		m_y = y;
	}
	Test(const Test & another)
	{
		cout << "Test(const Test &)..." << endl;
		m_x = another.m_x;
		m_y = another.m_y;
	}

	void printT() {
		cout << "x = " << m_x << ", m_y = " << m_y << endl;
	}

	~Test() {
		cout << "~Test()..." << endl;
	}
private:
	int m_x;
	int m_y;
};


Test func2()
{
	cout << "func2 begin..." << endl;
	Test temp(10, 20);
	temp.printT();

	cout << "func2 end..." << endl;

	return temp;
}//匿名的对象 = temp  匿名对象.拷贝构造(temp)


void test6()
{
	cout << "test6 begin..." << endl;
	Test t1;//t1已经被初始化了。

	t1 = func2(); //t1已经被初始化了,所以func2返回的匿名对象不会再次转正,而依然是匿名对象。
					//所以t1会调用等号操作符,t1.operator=(匿名对象), 然后编译器会立刻回收掉匿名对象

	t1.printT();

	cout << "test6 end.." << endl;
}


int main(void)
{
	
	test6();

	return 0;
}

运行结果!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值