深入理解C++类的构造函数与析构函数
在研究 C++ 類的繼承、派生、組合時,一直沒有清晰地了解構造函數與析構函數的調用過程。本章通過點 - 線組合類,來深入分析組合類情況下,對象的構造與析構。
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>程序執行結果:2.逐條語句進行分析?
<span style="font-size:18px;">Point mypoint1; </span> 該條語句是利用Point類進行初始化Point對象,此時,編譯器會自動調用程序的構造函數。由于對象定義不帶有任何參數,所以此時僅調用程序的默認構造函數。輸出為“Calling the default construction of Point!”
<span style="font-size:18px;">Point mypoint2(1,2);</span> 語句定義一個Point類的對象,但是此時我們應該注意到對mypoint2對象進行了初始化,此時操作系統會調用含有參數的構造函數。
具體行為過程是,在內存中開辟空間定義臨時變量 int xx, int yy;通過參數傳遞,xx = 1. yy = 2;然后在執行含有參數構造函數的函數體,完成臨時變量值向Point類的屬性x,y的賦值。
輸出“Calling the parameter construction of Point!”
<span style="font-size:18px;">Point mypoint3(mypoint2);</span>
語句仍然定義了一個Point類的對象,但是應該注意到,這里利用了前一個對象mypoint2對mypoint3進行初始化 。調用了Point類復制構造函數。相當于&pt = mypoint2 。此時,通過看看內存地址,我們可以看到,編譯器并沒用定義一個Point類的對象臨時pt。而是采用了“引用”的模式,pt僅僅是mypoint2的一個別名而已!!!
輸出“Calling the copy construction of point!”
<span style="font-size:18px;">Point mypt1(1,2),mypt2(3,4);</span>
定義Point類的兩個帶有初始化參數的對象,
輸出“Calling the parameter construction of Point!”
"Calling the parameter construction of Point!"
Line line1(mypt1,mypt2); 注意到,此時我們在嘗試利用參數傳遞的方式來構造Line的對象line1,也就是說,相比較而言Line是上一層的類,而mypt1/mypt2是底層類的對象。此時的程序執行過程可以分解為:
&pt = mypt2; ?// “Calling the copy construction of Point!”
&pt = mypt1; // "Calling the copy construction of Point!"
//注意到,此時仍然是引用的格式,沒有進行臨時對象的構建
line1(mypt1,mypt2); //“Calling the parameter construction of Line!”
重點:當我們利用底層類的對象對上一層類對象進行初始化之后,要立刻銷毀底層類的對象,及調用相應的析構函數。
“Calling the destructor of Point!” //銷毀對象mypt1
“Calling ?the destructor of Point!” //銷毀對象mypt2
Line line2(line1); 參考上例,此時的拷貝過程可以分解為:
&l = line1; //引用傳遞
“Calling the default construction of Point!”
"Calling the default construction of Point!"
//執行Line復制構造函數的函數體
“Calling the copy construction of Line”
退出程序后,對所有對象進行銷毀,其順序為“與構造函數調用順序相反”
總結
以上是生活随笔為你收集整理的深入理解C++类的构造函数与析构函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 写在开篇的话
- 下一篇: CComPtr对象作为参数进行 1.值传