c/c++整理析构函数

来源:互联网 发布:淘宝女人秋装 编辑:程序博客网 时间:2024/06/05 08:07

c++中虚析构函数的作用是什么?

  析构函数是为了在对象不被使用后释放它的资源,虚函数是为了实现多态。那么,把析构函数声明为virtual有什么作用呢? 请看下面代码:

[cpp] view plain copy
  1. <span style="font-size:18px;">#include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class Base  
  6. {  
  7. public:  
  8.     Base(){ }           //Base的构造函数  
  9.     ~Base()             //Base的析构函数  
  10.     {  
  11.         cout<<"output from the destructor of class Base"<<endl;  
  12.     }  
  13.     virtual void Dosomething()  
  14.     {  
  15.         cout<<"do something in class Base"<<endl;  
  16.     }     
  17. };  
  18.   
  19. class Derived : public Base  
  20. {  
  21. public:  
  22.     Derived(){ }        //Derived的构造函数  
  23.     ~Derived()          //Derived的析构函数  
  24.     {  
  25.         cout<<"output from the destructor of class Derived"<<endl;  
  26.     }  
  27.     void Dosomething()  
  28.     {  
  29.         cout<<"do something in class Derived"<<endl;  
  30.     }  
  31. };  
  32.   
  33. int main()  
  34. {  
  35.     Derived *pt1 = new Derived();  
  36.     pt1->Dosomething();  
  37.     delete pt1;  
  38.       
  39.     Base *pt2 = new Derived();  
  40.     pt2->Dosomething();  
  41.     delete pt2;  
  42.       
  43.     return 0;  
  44. }</span>  
程序输出:

[cpp] view plain copy
  1. <span style="font-size:18px;">do something in class Derived  
  2. output from the destructor of class Derived  
  3. output from the destructor of class Base  
  4. do something in class Derived  
  5. output from the destructor of class Base</span>  
  代码37行可以正常释放pt1的资源,但是代码41行并没有正常释放pt2的资源,从结果看,Derived类的析构函数并没有被调用。通常情况下,类的析构函数里面都是释放内存资源,而析构函数不被调用的话就会造成内存泄漏。原因是指针pt2是Base类型的指针,释放pt2时只进行Base类的析构函数。在代码第9行加上virtual关键字后:

[cpp] view plain copy
  1. do something in class Derived  
  2. output from the destructor of class Derived  
  3. output from the destructor of class Base  
  4. do something in class Derived  
  5. output from the destructor of class Derived  
  6. output from the destructor of class Base  
  此时释放指针pt2时,由于Base的析构函数是virtual的,就会先找到并执行Derived类的析构函数,然后执行Base类的析构函数,资源正常释放,避免了内存泄漏。

  因此,只有当一个类被用来作为基类的时候,才会把析构函数写成虚析构函数。



看代码写结果——析构函数的执行顺序

[cpp] view plain copy
  1. #include <iostream>  
  2.   
  3. using namespace std;  
  4.   
  5. class A  
  6. {  
  7. private:  
  8.     int a;  
  9. public:  
  10.     A(int aa) { a = aa; }  
  11.     ~A() { cout << "Destructor A!"<<a<<endl; }  
  12. };  
  13.   
  14. class B : public A  
  15. {  
  16. private:  
  17.     int b;  
  18. public:  
  19.     B(int aa=0, int bb=0):A(aa){ b = bb; }  
  20.     ~B(){ cout<<"Destructor B!"<<b<<endl;}  
  21. };  
  22.   
  23. int main()  
  24. {  
  25.     B obj1(5), obj2(6,7);  
  26.       
  27.     return 0;  
  28. }  
  本体考查的是析构函数的执行顺序。析构函数的执行顺序与构造函数的执行顺序相反。

  main()函数中定义了两个类B的对象,它们的基类是A。由于这两个对象都是栈中分配的,当main()函数退出时会发生析构,又因为obj1比obj2先声明,所以obj2先析构。它们的顺序是首先执行B的析构函数,然后执行A的析构函数。

程序输出:

[cpp] view plain copy
  1. Destructor B!7  
  2. Destructor A!6  
  3. Destructor B!0  
  4. Destructor A!5