33,311
社区成员
发帖
与我相关
我的任务
分享
"cc.h":
class CC
{
friend void swap(CC& lhs, CC& rhs);
public:
CC(std::string& s = std::string()) :ps(new std::string(s)),val(0) {
}
CC(const CC& c) : ps(new std::string(*c.ps)),val(c.val) {
}
CC(CC&& rc) noexcept :val(rc.val),ps(rc.ps) {
rc.ps = nullptr;
}
CC& operator= (const CC& rhs) {
CC tmp(rhs);
swap(*this, tmp);
return *this;
}
CC& operator=(CC&& rhs) {
swap(*this, rhs);
return *this;
}
//CC& operator= (CC rhs){
// swap(*this, rhs);
// return *this;
//}
~CC() {
delete ps;
ps = nullptr;
};
private:
std::string* ps;
int val;
};
inline void swap(CC& lhs, CC& rhs) {
std::swap(lhs.ps, rhs.ps);
std::swap(lhs.val, rhs.val);
}
"main.cpp":
CC getRF() {
return CC("from getRF");
}
void testRref() {
CC c1;
CC&& c2 = std::move(c1);
CC c3 = c2;
c3 = getRF();
CC c4 = std::move(c2);
CC c5 = "C++";
CC c6 = getRF();
}
CC(CC&& rc) :ps(nullptr),val(0) {
ps = rc.ps;
val = rc.val;
rc.ps = nullptr;
rc.val = 0;
}