1

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

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);

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

4

1 回答 1

4

要调用inherited operator=,您通常调用inherited operator=

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

不管是复制分配、移动分配还是某种用户定义的分配,模式都是一样的。

于 2018-06-14T10:51:58.970 回答