C++教程:C++虛函數(shù)與虛析構(gòu)函數(shù)
2020-05-23 14:25:22
供稿:網(wǎng)友
在類中,有兩個(gè)與眾不同的成員函數(shù),那就是構(gòu)造函數(shù)和析構(gòu)函數(shù)。當(dāng)構(gòu)造函數(shù)與析構(gòu)函數(shù)遭遇繼承和多態(tài),它們的運(yùn)行狀況又會(huì)出現(xiàn)什么變化呢?
多態(tài)性是在父類或各子類中執(zhí)行最合適成員函數(shù)。一般來說,只會(huì)選擇父類或子類中的某一個(gè)成員函數(shù)來執(zhí)行。這可給析構(gòu)函數(shù)帶來了麻煩!如果有的資源是父類的構(gòu)造函數(shù)申請(qǐng)的,有的資源是子類的構(gòu)造函數(shù)申請(qǐng)的,而虛函數(shù)只允許程序執(zhí)行父類或子類中的某一個(gè)析構(gòu)函數(shù),豈不是注定有一部分資源將無法被釋放?為了解決這個(gè)問題,虛析構(gòu)函數(shù)變得與眾不同。
下面我們就來給析構(gòu)函數(shù)的前面加上保留字virtual,看看運(yùn)行的結(jié)果會(huì)怎么樣:(程序17.8)
//animal.h
#include <iostream>
using namespace std;
class Animal
{
public:
Animal(int w=0,int a=0);
virtual ~Animal();//虛析構(gòu)函數(shù)
protected:
int weight,age;
};
Animal::Animal(int w,int a)
{
cout <<"Animal consturctor is running..." <<endl;
weight=w;
age=a;
}
Animal::~Animal()
{
cout <<"Animal destructor is running..." <<endl;
}
//cat.h
#include "animal.h"
class Cat:public Animal
{
public:
Cat(int w=0,int a=0);
~Cat();
};
Cat::Cat(int w,int a):Animal(w,a)
{
cout <<"Cat constructor is running..." <<endl;
}
Cat::~Cat()
{
cout <<"Cat destructor is running..." <<endl;
}
//main.cpp
#include "cat.h"
int main()
{
Animal *pa=new Cat(2,1);
Cat *pc=new Cat(2,4);
cout <<"Delete pa:" <<endl;
delete pa;
cout <<"Delete pc:" <<endl;
delete pc;
return 0;
}
運(yùn)行結(jié)果:
Animal consturctor is running...
Cat constructor is running...
Animal consturctor is running...
Cat constructor is running...
Delete pa:
Cat destructor is running...
Animal destructor is running...
Delete pc:
Cat destructor is running...
Animal destructor is running...
我們驚訝地發(fā)現(xiàn),虛析構(gòu)函數(shù)不再是運(yùn)行父類或子類的某一個(gè)析構(gòu)函數(shù),而是先運(yùn)行合適的子類析構(gòu)函數(shù),再運(yùn)行父類析構(gòu)函數(shù)。即兩個(gè)類的析構(gòu)函數(shù)都被執(zhí)行了,如果兩塊資源分別是由父類構(gòu)造函數(shù)和子類構(gòu)造函數(shù)申請(qǐng)的,那么使用了虛析構(gòu)函數(shù)之后,兩塊資源都能被及時(shí)釋放。
我們修改程序17.8,將Animal類析構(gòu)函數(shù)前的virtual去掉,會(huì)發(fā)現(xiàn)運(yùn)行結(jié)果中刪除pa指向的Cat對(duì)象時(shí),不執(zhí)行Cat類的析構(gòu)函數(shù)。如果這時(shí)Cat類的構(gòu)造函數(shù)里申請(qǐng)了內(nèi)存資源,就會(huì)造成內(nèi)存泄漏了。
所以說,虛函數(shù)與虛析構(gòu)函數(shù)的作用是不同的。虛函數(shù)是為了實(shí)現(xiàn)多態(tài),而虛析構(gòu)函數(shù)是為了同時(shí)運(yùn)行父類和子類的析構(gòu)函數(shù),使資源得以釋放。