[问题] 子类别move assignment

楼主: WangDaMing (王大明)   2021-01-02 00:19:14
开发平台(Platform): (Ex: Win10, Linux, ...)
Linux
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
GCC
问题(Question):
各位大大新年快乐
最近在看c++ move语意,看到子类别的move assignment需要呼叫父类别operator=
不懂为何需要这样
来源是看了stackoverflow的问题
https://reurl.cc/ld38MA
答案是
Derived &operator=(Derived &&rhs) {
Base::operator=(std::move(rhs));//***
// do the derived part
return *this;
}
想问***这行的用意是什么??完全想不懂为何要呼叫那个operator=
感谢大大回答
作者: loveme00835 (发箍)   2021-01-02 00:38:00
base type 和 data member (资料成员) 一样都是属于derived type 的 sub-object. 这就跟你得特别呼叫资料成员的 move assignment 一样, base type 也需要相同的操作. 要观察很简单, 在 base type 里新增资料成员并且在该成员型别的 move assignment 里印出讯息就好

Links booklink

Contact Us: admin [ a t ] ucptt.com