C++繼承中的對象構(gòu)造與析構(gòu)和賦值重載詳解
一、構(gòu)造/析構(gòu)順序及繼承性
class A { private: int _a; public: A(int a = 0): _a(a) { cout << "A()" << this << endl; } ~A() { cout << "~A()"<< this <<endl; } }; class B : public A { private: int _b; public: B(int b): _b(b), A() { cout << "B()" << this << endl; } ~B() { cout << "~B()"<< this <<endl; } };
結(jié)論:
1.構(gòu)造順序:先構(gòu)造基類,后構(gòu)造派生類
2.析構(gòu)順序:先析構(gòu)派生類,后析構(gòu)基類
二、拷貝構(gòu)造的繼承性
class A { private: int _a; public: A(int a = 0): _a(a) { cout << "A()" << this << endl; } A(const A& src): _a(src._a) { cout << "A(const A& src)"<< this << endl; } ~A() { cout << "~A()"<< this <<endl; } }; class B : public A { private: int _b; public: B(int b): _b(b), A() { cout << "B()" << this << endl; } B(const B& src): _b(src._b) { cout << "B(const B& src)" << this << endl; } ~B() { cout << "~B()"<< this <<endl; } };
結(jié)論:
1.先調(diào)用基類缺省的構(gòu)造函數(shù),后調(diào)用派生類的拷貝構(gòu)造函數(shù)
2.若派生類沒有缺省構(gòu)造函數(shù)A(),就會報錯
疑惑:如何去調(diào)用基類的拷貝構(gòu)造而不是缺省構(gòu)造
#include<iostream> using namespace std; class A { private: int _a; public: A(int a = 0) : _a(a) { cout << "A()" << this << endl; } A(const A& src) : _a(src._a) { cout << "A(const A& src)" << this << endl; } ~A() { cout << "~A()" << this << endl; } }; class B : public A { private: int _b; public: B(int b) : _b(b), A() { cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //發(fā)生賦值兼容規(guī)則(切片) { cout << "B(const B& src)" << this << endl; } ~B() { cout << "~B()" << this << endl; } }; int main() { B b(10); B b1(b); return 0; }
結(jié)果:
將B類型src傳遞給A類型的A(const A& src)拷貝構(gòu)造函數(shù),發(fā)生了賦值兼容規(guī)則(切片現(xiàn)象)
三、賦值重載不具有繼承性
#include<iostream> using namespace std; class A { private: int _a; public: A(int a = 0) : _a(a) { cout << "A()" << this << endl; } A(const A& src) : _a(src._a) { cout << "A(const A& src)" << this << endl; } A& operator=(const A& src) { if(this != &src) { _a = src._a; cout << "A& operator=(const A& src)" << endl; } } ~A() { cout << "~A()" << this << endl; } }; class B : public A { private: int _b; public: B(int b) : _b(b), A() { cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //發(fā)生賦值兼容規(guī)則(切片) { cout << "B(const B& src)" << this << endl; } B& operator=(const B& src) { if(this != &src) { _b = src._b; cout << "B& operator=(const B& src)" << endl; } } ~B() { cout << "~B()" << this << endl; } }; int main() { B b1(10); B b2(20); b1 = b2; return 0; }
結(jié)論:默認情況下僅僅調(diào)用了派生類的對象的賦值重載,并未調(diào)用基類的賦值重載。
解決方案:
#include<iostream> using namespace std; class A { private: int _a; public: A(int a = 0) : _a(a) { cout << "A()" << this << endl; } A(const A& src) : _a(src._a) { cout << "A(const A& src)" << this << endl; } A& operator=(const A& src) { if(this != &src) { _a = src._a; cout << "A& operator=(const A& src)" << endl; } } ~A() { cout << "~A()" << this << endl; } }; class B : public A { private: int _b; public: B(int b) : _b(b), A() { cout << "B()" << this << endl; } B(const B& src) : _b(src._b), A(src) //發(fā)生賦值兼容規(guī)則(切片) { cout << "B(const B& src)" << this << endl; } B& operator=(const B& src) { if(this != &src) { *(A*)this = src; //將調(diào)用基類賦值重載 _b = src._b; cout << "B& operator=(const B& src)" << endl; } } ~B() { cout << "~B()" << this << endl; } }; int main() { B b1(10); B b2(20); b1 = b2; return 0; }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
- C++構(gòu)造析構(gòu)賦值運算函數(shù)應用詳解
- C++淺析構(gòu)造函數(shù)的特性
- C++類與對象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
- C++超詳細講解構(gòu)造函數(shù)與析構(gòu)函數(shù)的用法及實現(xiàn)
- C++分析類的對象作類成員調(diào)用構(gòu)造與析構(gòu)函數(shù)及靜態(tài)成員
- C++分析構(gòu)造函數(shù)與析造函數(shù)的特點梳理
- 一起來學習C++的構(gòu)造和析構(gòu)
- C++編程析構(gòu)函數(shù)拷貝構(gòu)造函數(shù)使用示例詳解
- C++類的構(gòu)造與析構(gòu)特點及作用詳解
相關(guān)文章
C++實現(xiàn)LeetCode(161.一個編輯距離)
這篇文章主要介紹了C++實現(xiàn)LeetCode(161.一個編輯距離),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07C語言中system()執(zhí)行cmd命令打開關(guān)閉程序的方法
今天小編就為大家分享一篇C語言中system()執(zhí)行cmd命令打開關(guān)閉程序的方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05C語言設計圖書登記系統(tǒng)與停車場管理系統(tǒng)的實例分享
這篇文章主要介紹了C語言設計圖書登記系統(tǒng)與停車場管理系統(tǒng)的實例分享,重在以最簡單的一些需求來展示管理系統(tǒng)的設計思路,需要的朋友可以參考下2016-06-06C++ using namespace std 用法深入解析
以下是對C++中using namespace std的用法進行了詳細的分析介紹,需要的朋友可以過來參考下2013-07-07