【发布时间】:2014-01-09 06:50:01
【问题描述】:
假设我有这个课程:
class Test
{
public:
Test();
};
AFAIK,编译器提供默认的复制构造函数和赋值运算符,将其他实例的每个成员分配给当前实例。现在我添加移动构造函数和赋值:
class Test
{
public:
Test();
Test(Test&& other);
Test& operator=(Test&& other);
};
这个类是否仍然包含编译器生成的复制构造函数和赋值运算符,还是我需要实现它们?
编辑。这是我的测试:
class Test
{
public:
Test(){}
Test(Test&& other){}
Test& operator=(Test&& other)
{
return *this;
}
int n;
char str[STR_SIZE];
};
int main()
{
Test t1;
t1.n = 2;
strcpy(t1.str, "12345");
Test t2(t1);
Test t3;
t3 = t1;
cout << t2.n << " " << t2.str << " " << t3.n << " " << t3.str << endl;
return 0;
}
打印2 12345 2 12345。编译器:VC++ 2010。根据这个测试,拷贝构造函数和赋值还在。这是标准行为吗,我可以确定这适用于每个 C++ 编译器吗?
【问题讨论】:
标签: c++ c++11 move-semantics