#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();
}
std::move与右值引用
最新推荐文章于 2024-04-24 07:00:00 发布