国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

簡(jiǎn)單介紹C++編程中派生類的析構(gòu)函數(shù)

2020-01-26 14:51:22
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

和構(gòu)造函數(shù)類似,析構(gòu)函數(shù)也是不能被繼承的。

創(chuàng)建派生類對(duì)象時(shí),構(gòu)造函數(shù)的調(diào)用順序和繼承順序相同,先執(zhí)行基類構(gòu)造函數(shù),然后再執(zhí)行派生類的構(gòu)造函數(shù)。但是對(duì)于析構(gòu)函數(shù),調(diào)用順序恰好相反,即先執(zhí)行派生類的析構(gòu)函數(shù),然后再執(zhí)行基類的析構(gòu)函數(shù)。

請(qǐng)看下面的例子:

#include <iostream>using namespace std;class A{public: A(){cout<<"A constructor"<<endl;} ~A(){cout<<"A destructor"<<endl;}};class B: public A{public: B(){cout<<"B constructor"<<endl;} ~B(){cout<<"B destructor"<<endl;}};class C: public B{public: C(){cout<<"C constructor"<<endl;} ~C(){cout<<"C destructor"<<endl;}};int main(){ C test; return 0;}

運(yùn)行結(jié)果:

A constructorB constructorC constructorC destructorB destructorA destructor

從運(yùn)行結(jié)果可以很明顯地看出來(lái),構(gòu)造函數(shù)和析構(gòu)函數(shù)的執(zhí)行順序是相反的。

需要注意的是,一個(gè)類只能有一個(gè)析構(gòu)函數(shù),調(diào)用時(shí)不會(huì)出現(xiàn)二義性,所以析構(gòu)函數(shù)不需要顯式地調(diào)用。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 云阳县| 班戈县| 兴义市| 井冈山市| 郁南县| 涡阳县| 章丘市| 祁连县| 宁化县| 夏邑县| 西平县| 沙洋县| 衡南县| 龙岩市| 枣强县| 马边| 卓尼县| 钦州市| 石屏县| 四子王旗| 东乡族自治县| 塘沽区| 万荣县| 清镇市| 新巴尔虎右旗| 亚东县| 博乐市| 南投县| 襄汾县| 鹰潭市| 苍山县| 遵义市| 辽阳市| 攀枝花市| 东丰县| 萝北县| 双桥区| 东港市| 西吉县| 万州区| 清徐县|