網(wǎng)站首頁 編程語言 正文
一、構(gòu)造/析構(gòu)順序及繼承性
class A { private: int _a; public: A(int a = 0): _a(a) { cout << "A()" << this << endl; } ~A() { cout << "~A()"<< this <
結(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 <
結(jié)論:
1.先調(diào)用基類缺省的構(gòu)造函數(shù),后調(diào)用派生類的拷貝構(gòu)造函數(shù)
2.若派生類沒有缺省構(gòu)造函數(shù)A(),就會報錯
疑惑:如何去調(diào)用基類的拷貝構(gòu)造而不是缺省構(gòu)造
#includeusing 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)象)
三、賦值重載不具有繼承性
#includeusing 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é)論:默認(rèn)情況下僅僅調(diào)用了派生類的對象的賦值重載,并未調(diào)用基類的賦值重載。
解決方案:
#includeusing 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é)
原文鏈接:https://blog.csdn.net/xiaoxiaoguailou/article/details/123260864
相關(guān)推薦
- 2023-02-10 Redis如何正確關(guān)閉和開啟持久化_Redis
- 2023-01-07 MPAndroidChart自定義圖表Chart的Attribute及Render繪制邏輯_Andr
- 2022-01-13 macOS 升級后 nvm 安裝的 node 和 npm 出錯
- 2022-04-01 基于python,Matplotlib繪制函數(shù)的等高線與三維圖像_python
- 2022-10-14 yum-config-manager 命令找不到的解決方法
- 2023-01-06 linux?find命令將查找到的文件批量刪除方法_linux shell
- 2022-07-11 Python標(biāo)準(zhǔn)庫uuid模塊(生成唯一標(biāo)識)詳解_python
- 2022-07-21 List<Map<String, Object>>使用
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支