C++中移动赋值运算符的继承



我需要一些帮助来理解移动赋值运算符的继承过程。 对于给定的基类

class Base
{
public:
/* Constructors and other utilities */
/* ... */
/* Default move assignment operator: */
Base &operator=(Base &&) = default;
/* One can use this definition, as well: */
Base &operator=(Base &&rhs) {std::move(rhs); return *this;}
/* Data members in Base */
/* ... */
};
class Derived : public Base
{
public:
/* Constructors that include inheritance and other utilities */
/* ... */
Derived &operator=(Derived &&rhs);
/* Additional data members in Derived */
/* ... */
};

我不太确定如何在派生类中调用基本移动赋值运算符?我应该只使用范围运算符并说

Base::std:move(rhs);

然后是Derived类中定义的附加项的后续std::move(...),还是有其他方法?

要调用继承的operator=,你通常会调用继承的operator=

Derived &operator=(Derived &&rhs) {
Base::operator=(std::move(rhs));
// do the derived part
return *this;
}

无论是复制分配、移动分配还是某种用户定义的分配,模式都是相同的。

最新更新