Akh*_*aim 2 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 */
/* ... */
};
Run Code Online (Sandbox Code Playgroud)
我不太确定如何在派生类中调用基移动赋值运算符?我应该只使用作用域运算符并说
Base::std:move(rhs);
Run Code Online (Sandbox Code Playgroud)
接下来是类std::move(...)
中定义的附加项目的后续内容Derived
,或者还有其他方法吗?
要调用继承operator=
,通常需要调用继承operator=
。
Derived &operator=(Derived &&rhs) {
Base::operator=(std::move(rhs));
// do the derived part
return *this;
}
Run Code Online (Sandbox Code Playgroud)
无论是复制分配、移动分配还是某种用户定义的分配,模式都是相同的。