我正在编写自己的向量类(x,y值的容器),我不确定我应该自己实现什么构造函数/赋值运算符以及我可以依赖编译器提供的内容。显然,我需要编写任何没有默认行为或在任何情况下都不会自动生成的方法。但果然,如果编译器可以生成完全相同的东西,那么实现某些东西也没有意义。
我正在使用Visual Studio 2010(在C ++ 11方面可能很重要)。如果重要的话,我的班级也是一个模板。
目前,我有:
template <typename T>
class Integral2
{
// ...
Integral2(void)
: x(0), y(0)
{}
Integral2(Integral2 && other)
: x(std::move(other.x)), y(std::move(other.y))
{}
Integral2(T _x, T _y)
: x(_x), y(_y)
{}
Integral2 & operator =(Integral2 const & other)
{
x = other.x;
y = other.y;
return *this;
}
Integral2 & operator =(Integral2 && other)
{
x = std::move(other.x);
y = std::move(other.y);
return *this;
}
// ...
};
当我有移动ctor / move操作符时,是否需要复制ctor /赋值操作符?
答案 0 :(得分:1)
在C ++中,有Rule of Three,它为您应该根据您确定需要定义的构造函数/运算符提供指导。在C ++ 11中,有些人认为移动语义将三规则推到become the rule of five。查看this thread以获取示例实现。我建议您查看这些指南。