【发布时间】:2012-03-16 10:44:09
【问题描述】:
我正在 Visual Studio 11 上尝试一些新的 C++11 功能,从移动构造函数开始。我编写了一个名为“MyClass”的简单类,其中包含一个移动构造函数:
class MyClass
{
public:
explicit MyClass( int aiCount )
: mpiSize( new int( aiCount ) ),
miSize2( aiCount)
{
}
MyClass( MyClass&& rcOther )
: mpiSize( rcOther.mpiSize )
, miSize2( *rcOther.mpiSize )
{
rcOther.mpiSize = 0;
rcOther.miSize2 = 0;
}
~MyClass()
{
delete mpiSize;
}
private:
int *mpiSize;
int miSize2;
};
这里有问题:
- 我假设如果我不实现,编译器会为 MyClass 生成一个移动构造函数 - 但似乎不是这样?
- 移动构造函数的实现对于 MyClass 是否正确?
- 有没有更好的方法来实现 MyClass 的移动构造函数?
【问题讨论】: