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

标签 c++ inheritance move-assignment-operator

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

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

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

关于c++ - C++中移动赋值运算符的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50854598/

相关文章:

c++ - cc1.exe 系统错误 - 缺少 libwinpthread-1.dll - 但它不是

c++ - 在 C++ 中,当我需要使用枚举时,如何避免#include 头文件?

c++ - 如何评估应该在 C++ 中采用 Tcl_Obj 的 Tcl 表达式

c++ - 同一对象的双 move 是从左向右复制吗?

c++ - 为什么必须在运行时构造字符串?

Java OOP 优化代码

Java:从子类调用 super.getClass()

c++ - Sean Parent : for polymorphic types in an inheritance hierarchy, 具有可变对象是极端的异常(exception)

c++ - 关于 move 赋值运算符的问题

c++ - move 赋值运算符和`if(this!=&rhs)`