先上结论 ^ ^:(如有不对,请指正)
if (定义了 拷贝构造函数 、 拷贝赋值运算符 或 析构函数) {
不会合成移动构造函数和移动赋值运算符。此时会使用对应 拷贝 操作来代替 移动
}
else if (类的所有成员都可以移动) {
则会为类合成移动构造函数或移动赋值运算符。
}
else {
合成拷贝构造函数和拷贝复制运算符。
}
class Test {
public:
Test(int v, const string& str) {
p = new int(v);
s = str;
cout << "test()" << endl;
}
~Test() {
cout << "~Test()" << endl;
delete p;
p = nullptr;
}
private:
int* p = nullptr;
string s;
};
int main() {
Test c(10, "1234")