杂七杂八

来源:互联网 发布:调整优化作战力量结构 编辑:程序博客网 时间:2024/09/21 11:20
#include <iostream>using namespace std;class TestClass{friend ostream& operator<<(ostream&, TestClass&);public:TestClass(int *ptr) :p(ptr){}TestClass(const TestClass &value) : p(new int(*(value.p))) {}TestClass(TestClass &&value) :p(value.p) { value.p = nullptr; }TestClass& operator=(const TestClass& value){TestClass tmp(value);swap(tmp);cout << "copy" << endl;return *this;}TestClass& operator=(TestClass &&value){if (this != &value){delete p;p = value.p;value.p = nullptr;}cout << "move" << endl;return *this;}void swap(TestClass& other){using std::swap;swap(p, other.p);}private:int *p;};ostream& operator<<(ostream& os, TestClass& value){os << *(value.p);return os;}void swap(TestClass& a, TestClass& b){a.swap(b);}TestClass func(TestClass parameter){return parameter;}int main(){TestClass demo_1(new int(1));TestClass demo_2(new int(2));demo_1 = std::move(demo_2);//demo_1 = demo_2;cout << demo_1 << endl;demo_2 = func(new int(3));cout << demo_2 << endl;return 0;}

0 0
原创粉丝点击