C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
當(dāng)派生類定義了拷貝、賦值、移動操作時,該操作負責(zé)拷貝、賦值、移動包括基類部分成員在內(nèi)的整個對象。
1.1 定義派生類的拷貝或移到構(gòu)造函數(shù)
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 派生類析構(gòu)函數(shù):派生類析構(gòu)函數(shù)先執(zhí)行,然后執(zhí)行基類的析構(gòu)函數(shù)。
如果構(gòu)造函數(shù)或析構(gòu)函數(shù)調(diào)用了某個虛函數(shù),則應(yīng)該執(zhí)行與構(gòu)造函數(shù)或析構(gòu)函數(shù)所屬類型相對應(yīng)的虛函數(shù)版本。
【引用】
[1] 代碼oopTest.h
總結(jié)
以上是生活随笔為你收集整理的C++ Primer 5th笔记(chap 15 OOP)派生类的拷贝控制成员的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++ Primer 5th笔记(cha
- 下一篇: C++ Primer 5th笔记(cha