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

1         #include
2       using namespace std;
3
4       class Base
5       {
6       public:
7                Base() {};       //Base的构造函数
8               ~Base()        //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() {};     //Derived的构造函数
22              ~Derived()      //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类的析构函数,资源正常释放,避免了内存泄漏。
因此,只有当一个类被用来作为基类的时候,才会把析构函数写成虚函数。

原文: http://blog.csdn.net/han_348154920/article/details/5944351

virtual析构函数(作用)相关推荐

  1. Effective C++ .07 virtual析构函数的提供

    主要讲了, 1. virtual析构函数的作用与调用顺序 2. 使用时机,并不是使用了继承就要把基类的析构函数变为虚函数(virtual),只有当用于多态目的时才进行一个virtual析构函数的定义. ...

  2. C++ virtual 析构函数

    copy自:http://zxjgoodboy.blog.sohu.com/61482463.html 在此基础上稍作修改 C++中虚析构函数的作用 我们知道,用C++开发的时候,用来做基类的类的析构 ...

  3. Object的finalize()方法的作用是否与C++的析构函数作用相同

    Object的finalize()方法的作用是否与C++的析构函数作用相同 public class Finalization {private static Finalization finaliz ...

  4. 静态联编与动态联编之virtual的作用

    =========================定义========================= 将一个调用函数连接上正确的被调用函数,这个过程就叫做函数的联编,简称联编.在C++中,一共有两 ...

  5. 条款七 为多态基类声明virtual析构函数

    1.如果基类中的成员函数是virtual类型的,其继承类中相应的函数也是virtual类型,并且基类对象的引用指向继承类对象时, 基类就可以调用继承类函数,否则调用的是基类函数 class base ...

  6. 读书笔记_Effective_C++_条款七:为多态基类声明virtual析构函数

    严格来说,多态分为编译时多态和运行时多态,编译时多态是函数的重载,而运行时多态则是迟绑定技术,即根据基类指针或引用的实际指向来决定采取何种行动,一般来说,多态特指运行时多态.下面我们来举有关C++多态 ...

  7. effective C++读书笔记--【条款07:为多态基类声明virtual析构函数】

    问题 base class的指针p,指向一个derived class,如果: base class带着一个non-virtual析构函数: derived class对象经由这个base class ...

  8. virtual析构函数的作用?

    *************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com ****** ...

  9. C++ 虚析构函数作用

    简单的来说,虚函数的作用是当基类调用派生类的对象时,能够实现多态,即虚函数会优先调用派生类的对应函数. 那么虚析构函数的作用:就是释放派生类的内存,防止内存泄漏. 实例: 第一种情况,当没有虚析构函数 ...

最新文章

  1. nginx+keepalived
  2. VirtualBox uuid冲突问题
  3. 生成html页面的ftl文件,FreeMarker生成静态HTML页面的工具类FreeMarkerUtil
  4. 【学习笔记】35、定义自己的异常类
  5. ant中的table行列不对齐问题,以及换行,隐藏等问题
  6. Back to back销售订单和drop ship第三方销售订单的销售成本的问题
  7. 一张图学会python3高清图-用一张很丑的图学习Python数据可视化基础--热力图
  8. (2) GoJS Node简介
  9. python:rs, ws, es = select.select(inputs, [], []) --报错error 10022
  10. Android 修改字体,跳不过的 Typeface
  11. 华为路由器交换机基本配置方法
  12. Centos7二进制安装Mysql8.0.20
  13. 箱形图、盒须图、盒式图、箱线图
  14. jsp简介lamitry_[提拉米苏] 找人一起玩,今晚刚开的号
  15. 单板电源设计(LDO、DC/DC)
  16. 南京计算机类事业单位,南京市属事业单位公开招聘579人 3月25日起报名
  17. linux iptables实现单机多ip出口ip负载均衡(宽带叠加)
  18. C#、NPOI生成Word文档(模板)
  19. WEB前端关于SPA、MPA的区别
  20. hdu 6578 Blank dp

热门文章

  1. catia曲面设计从入门到精通_CATIA V5 曲面设计从入门到精通
  2. matlab r如何看曲线图,MATLAB 提取图片中的曲线数据重新画图
  3. 宿命传说2之女神召唤java_宿命传说2-女神的召唤
  4. gis 大屏_数据可视化大屏的前景如何
  5. python动态获取cookie_看到很多人求助python 我也求助一下如何写cookie的获取和登录吧...
  6. android 模拟crash_Android 收集Crash信息及用户操作步骤
  7. python获取网页元素坐标_html网页元素在屏幕上的坐标获取
  8. python的常用函数模块_(5)Python的常用模块函数
  9. PHP中的数组建必须为数字吗,PHP检查数组中缺少的数字
  10. 单片机ADC采样算法----平均值采样法