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. 基于eNSP的NAT/NAPT协议仿真实践

    一. 基本原理 eNSP(Enterprise Network Simulation Platform)是一款由华为提供的.可扩展的.图形化 操作的网络仿真工具平台,主要对企业网络路由器.交换机进行软 ...

  2. 『学了就忘』Linux基础命令 — 31、grep命令和通配符

    目录 1.grep命令介绍 2.find命令和grep命令的区别(重点) (1)find命令 (2)grep命令 3.通配符与正则表达式的区别 (1)通配符: (2)正则表达式: 1.grep命令介绍 ...

  3. adduser vs useradd

    Always use adduser (and deluser when deleting users) when you're creating new users from the command ...

  4. Redis高可用方案哨兵机制------ 配置文件sentinel.conf详解

    Redis的哨兵机制是官方推荐的一种高可用(HA)方案,我们在使用Redis的主从结构时,如果主节点挂掉,这时是不能自动进行主备切换和通知客户端主节点下线的. Redis-Sentinel机制主要用三 ...

  5. Windows漏洞:MS08-067远程代码执行漏洞复现及深度防御

    摘要:详细讲解MS08-067远程代码执行漏洞(CVE-2008-4250)及防御过程 本文分享自华为云社区<Windows漏洞利用之MS08-067远程代码执行漏洞复现及深度防御>,作者 ...

  6. Python如何格式化输出

    目录 Python中的格式化输出 1.旧格式化 2.新格式format( ) 函数 Python中的格式化输出 格式化输出就是将字符串中的某些内容替换掉再输出就是格式化输出 旧格式化输出常用的有%d( ...

  7. 菜鸡的Java笔记

    1.注释 在JAVA中对于注释有三种: 单行注释:// 多行注释:/*--*/ 文档注释:/**--*/ 2.关键字和标识符 在程序中描述类名称,方法名称,变量等概念就需要使用标识符来定义.而在JAV ...

  8. [atARC121D]1 or 2

    对于大小为1的集合,我们可以在其中加入0 因此,枚举0的个数,那么问题即可以看作要求每一个集合大小为2 (特别的,我们允许存在$\{0,0\}$,因为这样删除这两个0显然只会减小极差) 显然此时贪心将 ...

  9. [loj6500]操作

    差分,令$b_{i}=a_{i-1}\oplus a_{i}$,对于一个区间$[l,r]$,相当于令$a_{l-1}=a_{r+1}=0$之后求出$b_{l..r+1}$,对区间$[i-k,i)$异或 ...

  10. 异常处理截止和UML图

    0.异常处理机制 0.1.java中异常的作用是:增强程序健壮性. 0.2.java中异常以类和对象的形式存在. 1.java的异常处理机制 1.1.异常在java中以类和对象的形式存在.那么异常的继 ...