C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
當派生類定義了拷貝、賦值、移動操作時,該操作負責拷貝、賦值、移動包括基類部分成員在內的整個對象。
1.1 定義派生類的拷貝或移到構造函數
class Base { /* ... */ };class D : public Base{public:// by default, the base class default constructor initializes// the base part of an object// to use the copy or move constructor, we must explicitly call that// constructor in the constructor initializer listD(const D& d) : Base(d) // copy the base members/* initializers for members of D */ { /* ... */}D(D&& d) : Base(std::move(d)) // move the base members/* initializers for members of D */ { /* ... */}};1.2 派生類賦值運算符
派生類的賦值運算符必須顯式地為其基類部分賦值。
// Base::operator=(const Base&) is not invoked automatically D &D::operator=(const D &rhs) {Base::operator=(rhs); // assigns the base part// assign the members in the derived class, as usual,// handling self-assignment and freeing existing resources as appropriatereturn *this; }1.3 派生類析構函數:派生類析構函數先執行,然后執行基類的析構函數。
如果構造函數或析構函數調用了某個虛函數,則應該執行與構造函數或析構函數所屬類型相對應的虛函數版本。
【引用】
[1] 代碼oopTest.h
總結
以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha