Deleting a derived class object using a pointer to a base class that has a non-virtual destructor results in undefined behavior. To correct this situation, the base class should be defined with a virtual destructor.
  Source: https://www.securecoding.cert.org/confluence/display/cplusplus/OOP34-CPP.+Ensure+the+proper+destructor+is+called+for+polymorphic+objects

  For example, following program results in undefined behavior. Although the output of following program may be different on different compilers, when compiled using Dev-CPP, it prints following.
  Constructing base
  Constructing derived
  Destructing base

 1 // A program without virtual destructor causing undefined behavior
2 #include<iostream>
3
4 using namespace std;
5
6 class base
7 {
8 public:
9 base()
10 {
11 cout<<"Constructing base \n";
12 }
13 ~base()
14 {
15 cout<<"Destructing base \n";
16 }
17 };
18
19 class derived: public base
20 {
21 public:
22 derived()
23 {
24 cout<<"Constructing derived \n";
25 }
26 ~derived()
27 {
28 cout<<"Destructing derived \n";
29 }
30 };
31
32 int main(void)
33 {
34 derived *d = new derived();
35 base *b = d;
36 delete b;
37 getchar();
38 return 0;
39 }

  Making base class destructor virtual guarantees that the object of derived class is destructed properly, i.e., both base class and derived class destructors are called.

  For example, following program prints:
  Constructing base
  Constructing derived
  Destructing derived
  Destructing base

 1 // A program without virtual destructor causing undefined behavior
2 #include<iostream>
3
4 using namespace std;
5
6 class base
7 {
8 public:
9 base()
10 {
11 cout<<"Constructing base \n";
12 }
13 virtual ~base()
14 {
15 cout<<"Destructing base \n";
16 }
17 };
18
19 class derived: public base
20 {
21 public:
22 derived()
23 {
24 cout<<"Constructing derived \n";
25 }
26 ~derived()
27 {
28 cout<<"Destructing derived \n";
29 }
30 };
31
32 int main(void)
33 {
34 derived *d = new derived();
35 base *b = d;
36 delete b;
37 getchar();
38 return 0;
39 }

  As a guideline, any time you have a virtual function in a class, you should immediately add a virtual destructor (even if it does nothing). This way, you ensure against any surprises later.

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-26  21:06:36

Virtual Destructor的更多相关文章

  1. [C++] Virtual Destructor(虚析构函数)

    Without Virtual Destructor(虚析构函数) class A{ public: ; A() { cout <<"A()..."<< e ...

  2. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  3. effective c++ 条款7 declare virtual destructor for polymophyc base class

    这似乎很明显. 如果base class的destructor不是virtual,当其derived class作为基类使用,析构的时候derived class的数据成员将不会被销毁. 举个例子 我 ...

  4. C++ 虚析构(virtual destructor)原理

    注意:本文仅为个人理解,可能有误! 先看一段代码: #include <iostream> using namespace std; class CBase{ public: CBase( ...

  5. 《Effective C++》条款14 总是让base class拥有virtual destructor

    有时,一个类想跟踪它有多少个对象存在.一个简单的方法是创建一个静态类成员来统计对象的个数.这个成员被初始化为0,在构造函数里加1,析构函数里减1.(条款m26里说明了如何把这种方法封装起来以便很容易地 ...

  6. 为什么内联函数,构造函数,静态成员函数不能为virtual函数

    http://blog.csdn.net/freeboy1015/article/details/7635012 为什么内联函数,构造函数,静态成员函数不能为virtual函数? 1> 内联函数 ...

  7. 条款7:为多态基类声明virtual析构函数

    C++明确指出:当派生类对象是由一个基类指针释放的,而基类中的析构函数不是虚函数,那么结果是未定义的.其实我们执行时其结果就是:只调用最上层基类的析构函数,派生类及其中间基类的析构函数得不到调用. # ...

  8. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  9. why pure virtual function has definition 为什么可以在基类中实现纯虚函数

    看了会音频,无意搜到一个frameworks/base/include/utils/Flattenable.h : virtual ~Flattenable() = 0; 所以查了下“纯虚函数定义实现 ...

随机推荐

  1. DDTP 分布式数据传输协议白皮书

    声明 本文非本人原创,主要参考文献[1]编写的阅读笔记.本博客仅发表在博客园,作者LightningStar,其他平台均为转载. 摘要 本白皮书对全球现有主要个人信息可携带权的实践模式进行梳理,分析其 ...

  2. Flink 实践教程 - 入门(4):读取 MySQL 数据写入到 ES

    ​作者:腾讯云流计算 Oceanus 团队 流计算 Oceanus 简介 流计算 Oceanus 是大数据产品生态体系的实时化分析利器,是基于 Apache Flink 构建的具备一站开发.无缝连接. ...

  3. 多线程 | 03 | CAS机制

    Compare and swap(CAS) 当前的处理器基本都支持CAS,只不过每个厂家所实现的算法并不一样罢了,每一个CAS操作过程都包含三个参数:一个内存地址V,一个期望的值A和一个新值B,操作的 ...

  4. 你以为我在玩游戏?其实我在学 Java

    大家好,我是程序员cxuan!今天和大家一起Look一下这个有趣的国外编程网站! 寓教于乐 "今天我们来学习 Java " . "Java 是一门面向对象的编程语言&qu ...

  5. OOP 4.21晚 指针知识点

    1.读法:int* ptr ptr是一个指针指向整型变量 2.指针类型:指针声明语句里的指针名字去掉,剩下的部分就是这个指针的类型; 3.指针所指向的类型:只须把指针声明语句中的指针名字和名字左边的指 ...

  6. 运行脚本 结果出现 Vim: Warning 并且卡住不能输入其它命令

    当我在执行一个 关于执行linux操作的php脚本时,就出现了以下信息:"Vim: Warning: Output is not to a terminal",接着出现了一大堆的字 ...

  7. 体验用yarp连接websocket

    前段时间一看yarp的仓库,wow,终于发布1.0版本了..net也升级到6版本了,之前一直只是用yarp做HTTP转发,今天刚好试试websocket 话不多说,直接开搞 配置集群 首先先配置集群信 ...

  8. 微信小程序(六)

    MINA 框架: 启动: 冷启动,热启动 加载: 生命周期 路由: 事件: 事件时视图层到逻辑层的通信方式 事件可以将用户的行为反馈到逻辑层进行处理 事件可以绑定在组件上,触发事件后就会执行逻辑层对应 ...

  9. cesium开发(1)搭建 vue + cesium开发环境

    进入新公司一段时间了,新公司业务主要从事卫星方面等webgl的开发,主要使用了leafletjs和cesium,其中cesium难度较大,需求较多,再进行了一段时间的使用开发后依旧感到有些力不从心, ...

  10. 前端---梳理 http 知识体系 2

    为什么要有HTTPS HTTP 天生具有明文的特点,整个传输过程完全透明,任何人都能够在链路中截获.修改或者伪造请求 / 响应报文,数据不具有安全性.仅凭HTTP 自身是无法解决的,需要引入新的HTT ...