std::move与右值引用

#include <iostream>
#include <string>
using namespace std;

class Test
{
public:
	Test()
	{
		m_str = new char[10];
		strcpy_s(m_str, 5, "abcd");
		cout << "Test Constructor" << endl;
	}
	Test(const Test& other) 
	{
		m_str = new char[10];
		strcpy_s(m_str, 5, "abcd");
		cout << "Test Copy Constructor" << endl;
	}
	Test(Test&& other)
	{
		m_str = other.m_str;
		other.m_str = NULL;
		cout << "Test Move Constructor" << endl;
	}
	char* m_str;
};


int main()
{
	Test a;
	Test b(a);
	cout << "a.m_str = " << a.m_str << endl;
	cout << "b.m_str = " << b.m_str << endl;

	cout << "==============" << endl;

	Test d;
	Test e(std::move(d));
	if(d.m_str == NULL)
		cout << "d.m_str = NULL"  << endl;
	cout << "e.m_str = " << e.m_str << endl;

	cout << "==============" << endl;

	string   s1 = "Hello ";
	string   s2 = "world";
	string&& s_rref = s1 + s2;   
	s_rref += ", my friend";          
	cout << s_rref << endl;

	cout << "==============" << endl;

	string   s3 = "Hello";
	string   s4(s3);
	cout << "s3 =" << s3 <<  endl;
	cout << "s4 =" << s4 << endl;

	cout << "==============" << endl;

	string   s5 = "Hello";
	string   s6(std::move(s5));
	cout << "s5 =" << s5 << endl;
	cout << "s6 =" << s6 << endl;

	getchar();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值