重學C++ 移動語義
阿新 • • 發佈:2018-12-18
#include "iostream"
#include "vector"
#include <algorithm>
using namespace std;
class Test
{
public:
Test() = default;
Test(const Test& t) = default;
Test(Test&& t)
{
if (this == &t) return;
this->x = t.x;
this->y = t.y;
this ->z = t.z;
this->ptr = t.ptr;
t.ptr = nullptr;
cout << "Test(Test&& t) " << endl;
}
int x, y;
int z;
char* ptr;
};
Test foo(Test t2)
{
return t2;
}
int main()
{
Test t2;
t2.x = 1;
t2.y = 2;
t2.z = 3;
Test t = foo(t2);
cout << t.x << " " << t.y << " " << t.z << endl;
getchar();
return 0;
}