C++中虚构函数的作用
时间:2010-10-28 来源:cdlda
析构函数是为了在对象不被使用之后释放它的资源,虚函数是为了实现多态。那么把析构函数声明为vitual有什么作用呢?请看下面的代码:
1 #include <iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 Base() {};
8 ~Base()
9 {
10 cout << "Output from the destructor of class Base!" << endl;
11 };
12 virtual void DoSomething()
13 {
14 cout << "Do something in class Base!" << endl;
15 };
16 };
17
18 class Derived : public Base
19 {
20 public:
21 Derived() {};
22 ~Derived()
23 {
24 cout << "Output from the destructor of class Derived!" << endl;
25 };
26 void DoSomething()
27 {
28 cout << "Do something in class Derived!" << endl;
29 };
30 };
31
32 int main()
33 {
34 Derived *pTest1 = new Derived(); //Derived类的指针
35 pTest1->DoSomething();
36 delete pTest1;
37
38 cout << endl;
39
40 Base *pTest2 = new Derived(); //Base类的指针
41 pTest2->DoSomething();
42 delete pTest2;
43
44 return 0;
45 }
先看程序输出结果:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Base!
代码第36行可以正常释放pTest1的资源,而代码第42行没有正常释放pTest2的资源,因为从结果看Derived类的析构函数并没有被调用。通常情况下类的析构函数里面都是释放内存资源,而析构函数不被调用的话就会造成内存泄漏。原因是指针pTest2是Base类型的指针,释放pTest2时 只进行Base类的析构函数。在代码第8行前面加上virtual关键字后的运行结果如下:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Derived!
7 Output from the destructor of class Base!
此时释放指针pTest2时,由于Base的析构函数是virtual的,就会先找到并执行Derived类的析构函数,然后再执行Base类的析构函数,资源正常释放,避免了内存泄漏。
因此,只有当一个类被用来作为基类的时候,才会把析构函数写成虚函数。
1 #include <iostream>
2 using namespace std;
3
4 class Base
5 {
6 public:
7 Base() {};
8 ~Base()
9 {
10 cout << "Output from the destructor of class Base!" << endl;
11 };
12 virtual void DoSomething()
13 {
14 cout << "Do something in class Base!" << endl;
15 };
16 };
17
18 class Derived : public Base
19 {
20 public:
21 Derived() {};
22 ~Derived()
23 {
24 cout << "Output from the destructor of class Derived!" << endl;
25 };
26 void DoSomething()
27 {
28 cout << "Do something in class Derived!" << endl;
29 };
30 };
31
32 int main()
33 {
34 Derived *pTest1 = new Derived(); //Derived类的指针
35 pTest1->DoSomething();
36 delete pTest1;
37
38 cout << endl;
39
40 Base *pTest2 = new Derived(); //Base类的指针
41 pTest2->DoSomething();
42 delete pTest2;
43
44 return 0;
45 }
先看程序输出结果:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Base!
代码第36行可以正常释放pTest1的资源,而代码第42行没有正常释放pTest2的资源,因为从结果看Derived类的析构函数并没有被调用。通常情况下类的析构函数里面都是释放内存资源,而析构函数不被调用的话就会造成内存泄漏。原因是指针pTest2是Base类型的指针,释放pTest2时 只进行Base类的析构函数。在代码第8行前面加上virtual关键字后的运行结果如下:
1 Do something in class Derived!
2 Output from the destructor of class Derived!
3 Output from the destructor of class Base!
4
5 Do something in class Derived!
6 Output from the destructor of class Derived!
7 Output from the destructor of class Base!
此时释放指针pTest2时,由于Base的析构函数是virtual的,就会先找到并执行Derived类的析构函数,然后再执行Base类的析构函数,资源正常释放,避免了内存泄漏。
因此,只有当一个类被用来作为基类的时候,才会把析构函数写成虚函数。
相关阅读 更多 +