深入理解C++类的构造函数与析构函数
在研究 C++ 類的繼承、派生、組合時(shí),一直沒有清晰地了解構(gòu)造函數(shù)與析構(gòu)函數(shù)的調(diào)用過程。本章通過點(diǎn) - 線組合類,來深入分析組合類情況下,對(duì)象的構(gòu)造與析構(gòu)。
1.問題的引入
源代碼: <span style="font-size:18px;">#include <iostream> using namespace std; #include<math.h>class Point { private:int x; int y;public:int getx();int gety();Point(){ cout<<"Calling the default construction of Point!"<<endl; }Point(int xx, int yy){ cout<<"Calling the parameter construction of Point!"<<endl; x = xx; y = yy;}Point(Point &pt){ cout<<"Calling the copy construction of Point!"<<endl; x = pt.x; y = pt.y;}~Point(){cout<<"Calling the destructor of Point!"<<endl;} }; int Point::getx() { return x;} int Point::gety() { return y;} //================================================================== class Line { private:Point dot1,dot2;double Len; public:Line(){cout<<"Calling the default construction of Line!"<<endl;}Line(Point pt1, Point pt2){cout<<"Calling the parameter construction of Line"<<endl;dot1 = pt1; dot2 = pt2;double Linex = abs( dot1.getx() - dot2.getx() );double Liney = abs( dot1.gety() - dot2.gety() );Len = sqrt( Linex*Linex + Liney*Liney);}Line(Line &l){cout<<"Calling the copy construction of Line"<<endl;Len = l.Len;}~Line(){ cout<<"Calling the destructor of Line!"<<endl;} }; void main() {Point mypoint1; Point mypoint2(1,2);Point mypoint3(mypoint2);cout<<mypoint2.getx()<<endl;cout<<mypoint3.gety()<<endl;Point mypt1(1,2),mypt2(3,4);Line line1(mypt1,mypt2);Line line2(line1);}</span>程序執(zhí)行結(jié)果:2.逐條語句進(jìn)行分析?
<span style="font-size:18px;">Point mypoint1; </span> 該條語句是利用Point類進(jìn)行初始化Point對(duì)象,此時(shí),編譯器會(huì)自動(dòng)調(diào)用程序的構(gòu)造函數(shù)。由于對(duì)象定義不帶有任何參數(shù),所以此時(shí)僅調(diào)用程序的默認(rèn)構(gòu)造函數(shù)。輸出為“Calling the default construction of Point!”
<span style="font-size:18px;">Point mypoint2(1,2);</span> 語句定義一個(gè)Point類的對(duì)象,但是此時(shí)我們應(yīng)該注意到對(duì)mypoint2對(duì)象進(jìn)行了初始化,此時(shí)操作系統(tǒng)會(huì)調(diào)用含有參數(shù)的構(gòu)造函數(shù)。
具體行為過程是,在內(nèi)存中開辟空間定義臨時(shí)變量 int xx, int yy;通過參數(shù)傳遞,xx = 1. yy = 2;然后在執(zhí)行含有參數(shù)構(gòu)造函數(shù)的函數(shù)體,完成臨時(shí)變量值向Point類的屬性x,y的賦值。
輸出“Calling the parameter construction of Point!”
<span style="font-size:18px;">Point mypoint3(mypoint2);</span>
語句仍然定義了一個(gè)Point類的對(duì)象,但是應(yīng)該注意到,這里利用了前一個(gè)對(duì)象mypoint2對(duì)mypoint3進(jìn)行初始化 。調(diào)用了Point類復(fù)制構(gòu)造函數(shù)。相當(dāng)于&pt = mypoint2 。此時(shí),通過看看內(nèi)存地址,我們可以看到,編譯器并沒用定義一個(gè)Point類的對(duì)象臨時(shí)pt。而是采用了“引用”的模式,pt僅僅是mypoint2的一個(gè)別名而已!!!
輸出“Calling the copy construction of point!”
<span style="font-size:18px;">Point mypt1(1,2),mypt2(3,4);</span>
定義Point類的兩個(gè)帶有初始化參數(shù)的對(duì)象,
輸出“Calling the parameter construction of Point!”
"Calling the parameter construction of Point!"
Line line1(mypt1,mypt2); 注意到,此時(shí)我們?cè)趪L試?yán)脜?shù)傳遞的方式來構(gòu)造Line的對(duì)象line1,也就是說,相比較而言Line是上一層的類,而mypt1/mypt2是底層類的對(duì)象。此時(shí)的程序執(zhí)行過程可以分解為:
&pt = mypt2; ?// “Calling the copy construction of Point!”
&pt = mypt1; // "Calling the copy construction of Point!"
//注意到,此時(shí)仍然是引用的格式,沒有進(jìn)行臨時(shí)對(duì)象的構(gòu)建
line1(mypt1,mypt2); //“Calling the parameter construction of Line!”
重點(diǎn):當(dāng)我們利用底層類的對(duì)象對(duì)上一層類對(duì)象進(jìn)行初始化之后,要立刻銷毀底層類的對(duì)象,及調(diào)用相應(yīng)的析構(gòu)函數(shù)。
“Calling the destructor of Point!” //銷毀對(duì)象mypt1
“Calling ?the destructor of Point!” //銷毀對(duì)象mypt2
Line line2(line1); 參考上例,此時(shí)的拷貝過程可以分解為:
&l = line1; //引用傳遞
“Calling the default construction of Point!”
"Calling the default construction of Point!"
//執(zhí)行Line復(fù)制構(gòu)造函數(shù)的函數(shù)體
“Calling the copy construction of Line”
退出程序后,對(duì)所有對(duì)象進(jìn)行銷毀,其順序?yàn)椤芭c構(gòu)造函數(shù)調(diào)用順序相反”
總結(jié)
以上是生活随笔為你收集整理的深入理解C++类的构造函数与析构函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写在开篇的话
- 下一篇: 静态链接中的那点事儿(2):C++二进制