1、作用:
在实现多态时,当用基类的指针操作派生类,在析构时防止只析构基类而不析构派生类的状况发生。!!!!
2、实例:
#include <iostream>
using namespace std;
class father
{
public:
father()
{
mPtr = new int;
}
~father()//(1)非虚函数
{
delete mPtr;
cout << "father Destruction......" << endl;
}
private:
int *mPtr;
};
class son:public father
{
public:
son()
{
mStr = new long;
}
~son()
{
delete mStr;
cout << "son Destruction......" << endl;
}
private:
long *mStr;
};
int main()
{
father *p = new son;//(2)基类指针操作子类
delete p;
return 0;
}
结果:
father Destruction......
Process returned 0 (0x0) execution time : 0.023 s
Press any key to continue.
从程序的运行结果来看,程序最后只释放了父类的内存,子类的内存并没有释放。则这段程序产生了内存泄露。!!!那是什么原因导致的呢?
在main函数中new出来的是子类son的对象,采用一个父类father的指针来接收,故在析构的时候,编译器因为只知道这个指针是父类的,所以只将父类部分的内存析构了,而不会去析构子类的内存,就造成了内存泄露,那么如何避免这种情况的产生呢?
将父类的析构函数改为虚函数,就可以避免这种情况。!!!
只需将上例的(1)处非虚函数改为:
virtual ~father()
{
delete mPtr;
cout << "father Destruction......" << endl;
}
运行得到结果:
son Destruction......
father Destruction......
Process returned 0 (0x0) execution time : 0.025 s
Press any key to continue.
从程序的运行结果可以看出,父类和子类的内存都被析构了。所以在使用多态时一定要将父类的析构函数定义成虚函数,从而避免内存泄露。!!!
如果不需要基类对派生类及对象进行操作,则不能定义虚函数,因为这样会增加内存开销.当类里面有定义虚函数的时候,编译器会给类添加一个虚函数表,里面来存放虚函数指针,这样就会增加类的存储空间.所以,只有当一个类被用来作为基类的时候,才把析构函数写成虚函数.!!!
对于如下代码,在(2)处,只是使用子类指针操作子类,则没必要将基类析构函数定义为虚函数:
#include <iostream>
using namespace std;
class father
{
public:
father()
{
mPtr = new int;
}
~father()//(1)非虚函数
{
delete mPtr;
cout << "father Destruction......" << endl;
}
private:
int *mPtr;
};
class son:public father
{
public:
son()
{
mStr = new long;
}
~son()
{
delete mStr;
cout << "son Destruction......" << endl;
}
private:
long *mStr;
};
int main()
{
son *p = new son; //(2)子类指针操作子类
delete p;
return 0;
}
结果:
son Destruction......
father Destruction......
Process returned 0 (0x0) execution time : 0.038 s
Press any key to continue.