C++基礎(chǔ)之析構(gòu)函數(shù)的介紹
析構(gòu)函數(shù)(destructor) 與構(gòu)造函數(shù)相反,當(dāng)對(duì)象脫離其作用域時(shí)(例如對(duì)象所在的函數(shù)已調(diào)用完畢),系統(tǒng)自動(dòng)執(zhí)行析構(gòu)函數(shù)。析構(gòu)函數(shù)往往用來做“清理善后” 的工作(例如在建立對(duì)象時(shí)用new開辟了一片內(nèi)存空間,應(yīng)在退出前在析構(gòu)函數(shù)中用delete釋放)。
前面的一些例子都沒有說明析構(gòu)函數(shù),這是因?yàn)樗玫降念愒诮Y(jié)束時(shí)不需要做特別的清理工作。下面的程序給出了一新的Date類,其中包括一個(gè)字符串指針,用來表示月份。
- #include \"iostream.h\"
- #include \"string.h\"
- class Date
- {
- int mo,da,yr;
- char *month;
- public:
- Date(int m=0, int d=0, int y=0);
- ~Date();
- void display() const;
- };
- Date::Date(int m,int d,int y)
- {
- static char *mos[] =
- {
- \"January\",\"February\",\"March\",\"April\",\"May\",\"June\",
- \"July\",\"August\",\"September\",\"October\",\"November\",\"December\"
- };
- mo=m; da=d; yr=y;
- if(m!=0)
- {
- month=new char[strlen(mos[m-1])+1];
- strcpy(month, mos[m-1]);
- }
- else month = 0;
- }
- Date::~Date()
- {
- delete [] month;
- }
- void Date::display() const
- {
- if(month!=0) cout<<month<<\' \'<<da<<\',\'<<yr;
- }
- int main()
- {
- Date birthday(8,11,1979);
- birthday.display();
- return 0;
- }
在Date對(duì)象的構(gòu)造函數(shù)中,首先用new運(yùn)算符為字符串month動(dòng)態(tài)分配了內(nèi)存,然后從內(nèi)部數(shù)組中把月份的名字拷貝給字符串指針month。
析構(gòu)函數(shù)在刪除month指針時(shí),可能會(huì)出現(xiàn)一些問題。當(dāng)然從這個(gè)程序本身來看,沒什么麻煩;但是從設(shè)計(jì)一個(gè)類的角度來看,當(dāng)Date類用于賦值時(shí),就會(huì)出現(xiàn)問題。假設(shè)上面的main()修改為“
- int main()
- {
- Date birthday(8,11,1979);
- Date today;
- today=birthday;
- birthday.display();
- return 0;
- }
這會(huì)生成一個(gè)名為today的空的Date型變量,并且把birthday值賦給它。如果不特別通知編譯器,它會(huì)簡(jiǎn)單的認(rèn)為類的賦值就是成員對(duì)成員的拷貝。在上面的程序中,變量birthday有一個(gè)字符型指針month,并且在構(gòu)造函數(shù)里用new運(yùn)算符初始化過了。當(dāng)birthday離開其作用域時(shí),析構(gòu)函數(shù)會(huì)調(diào)用delete運(yùn)算符來釋放內(nèi)存。但同時(shí),當(dāng)today離開它的作用域時(shí),析構(gòu)函數(shù)同樣會(huì)對(duì)它進(jìn)行釋放操作,而today里的month指針是birthday里的month指針的一個(gè)拷貝。析構(gòu)函數(shù)對(duì)同一指針進(jìn)行了兩次刪除操作,這會(huì)帶來不可預(yù)知的后果。
如果假設(shè)today是一個(gè)外部變量,而birthday是一個(gè)自變量。當(dāng)birthday離開其作用域時(shí),就已經(jīng)把對(duì)象today里的month指針刪除了。顯然這也是不正確的。
再假設(shè)有兩個(gè)初始化的Date變量,把其中一個(gè)的值賦值給另一個(gè):
- Date birthday(8,11,1979);
- Date today(12,29,2003);
- today=birthday;
問題就更復(fù)雜了,當(dāng)這兩個(gè)變量離開作用域時(shí),birthday中的month的值已經(jīng)通過賦值傳遞給了today。而today中構(gòu)造函數(shù)用new運(yùn)算符給month的值卻因?yàn)橘x值被覆蓋了。這樣,birthday中的month被刪除了兩次,而today中month卻沒有被刪除掉。
希望以上內(nèi)容對(duì)析構(gòu)函數(shù)的介紹,能夠給你帶來幫助。