#include <iostream>
#include <string>
using namespace std;
class Example6 {
string* ptr;
public:
Example6 (const string& str) : ptr(new string(str)) {}
~Example6 () {delete ptr;}
//move constructor
Example6 (Example6&& x) : ptr(x.ptr) {x.ptr = nullptr;}
//move assignment
Example6& operator= (Example6&& x) {
delete ptr;
ptr = x.ptr;
x.ptr = nullptr;
return *this;
}
//access content:
const string& content () const {return *ptr;}
//addtion:
Example6 operator+(const Example6& rhs) {
return Example6(content()+rhs.content() );
}
};
int main(int argc, char const *argv[])
{
Example6 foo ("Exam");
Example6 bar = Example6("ple");
foo = foo + bar;
cout << "foo's content: " << foo.content() << "\n";
return 0;
}
c++中move constructor and assignment
最新推荐文章于 2025-03-28 20:15:00 发布