在C++引物第5版。据说:
“基类需要虚拟析构函数这一事实对基类和派生类的定义有着重要的间接影响:如果一个类定义了析构函数--即使它使用=默认值使用合成版本--编译器也不会为该类合成移动运算符(§13.6.2,第537页)。”
为了获得更多的理解,我尝试了这样的方法:
class A
{
public:
A(){cout << "A::A()\n";}
virtual ~A(){cout << "A::~A()\n";}
A& operator = (const A&) {cout << "A::=\n"; return *this;};
A& operator = (A&&) = default;
};
int main()
{
A a;
A a2;
a2 = std::move(a); // why copy-assignment operator is not called here. Normally a will be "moved" through copy-ctor (moving through working copy-ctor)
std::cout << std::endl;
}class A有一个virtual析构函数,因此编译器不会合成移动ctor/赋值操作,但是在main中,我尝试移动一个class A类型的对象,所以通常move这里使用复制赋值而不是移动赋值,但是这里不调用复制分配?!发布于 2020-07-18 23:02:03
你的教科书是对的,编译器不会自动合成一个移动操作符。在代码示例中,您通过将编译器设置为默认来强制编译器生成一个,只有在隐式删除move运算符时才会失败。而不是含蓄地声明。
隐式删除准则
隐式声明准则
https://stackoverflow.com/questions/62974462
复制相似问题