開發平台(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=
感謝大大回答
base type 和 data member (資料成員) 一樣都是屬於derived type 的 sub-object. 這就跟你得特別呼叫資料成員的 move assignment 一樣, base type 也需要相同的操作. 要觀察很簡單, 在 base type 裡新增資料成員並且在該成員型別的 move assignment 裡印出訊息就好