我使用VS11并使用以下内容:
class ContextWrapper
{
public:
ContextWrapper()
{
} //it should be defaulted I *guess* in order to have automatic move constructor ?
// no support in VS11 for that now
Context* GetContext()
{
return this->context.get();
}
void SetContext(std::unique_ptr<Context> context)
{
this->context = std::move(context);
}
//ContextWrapper(ContextWrapper&& other): context(std::move(other.context))
//{
//} // I would like this to be generated by the compiler
private:
ContextWrapper(const ContextWrapper&);
ContextWrapper& operator= (const ContextWrapper&);
std::unique_ptr<Context> context;
};
Run Code Online (Sandbox Code Playgroud)
我希望这个类生成移动构造函数/赋值.事实是我没有一个琐碎的构造函数,因为我没有动作吗?还是有其他因素影响这个?
假设我有这个课程:
class Test
{
public:
Test();
};
Run Code Online (Sandbox Code Playgroud)
AFAIK,编译器提供默认的复制构造函数和赋值运算符,它将其他实例的每个成员分配给当前实例.现在我添加移动构造函数和赋值:
class Test
{
public:
Test();
Test(Test&& other);
Test& operator=(Test&& other);
};
Run Code Online (Sandbox Code Playgroud)
该类是否仍包含编译器生成的复制构造函数和赋值运算符,或者我需要实现它们?
编辑.这是我的测试:
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; …Run Code Online (Sandbox Code Playgroud)