zoukankan      html  css  js  c++  java
  • Virtual Destructor

      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 
    "; 
    12     }
    13     ~base()
    14     { 
    15         cout<<"Destructing base 
    "; 
    16     }     
    17 };
    18 
    19 class derived: public base 
    20 {
    21 public:
    22     derived()     
    23     { 
    24         cout<<"Constructing derived 
    "; 
    25     }
    26     ~derived()
    27     { 
    28         cout<<"Destructing derived 
    "; 
    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 
    "; 
    12     }
    13     virtual ~base()
    14     { 
    15         cout<<"Destructing base 
    "; 
    16     }     
    17 };
    18 
    19 class derived: public base 
    20 {
    21 public:
    22     derived()     
    23     { 
    24         cout<<"Constructing derived 
    "; 
    25     }
    26     ~derived()
    27     { 
    28         cout<<"Destructing derived 
    "; 
    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

  • 相关阅读:
    大数据课上用spark
    Python 机器学习及实践 Codeing 模型实用技巧 (特征提升 模型正则化 模型检测 超参数搜索)
    学习网站保存
    Tensorflow + Keras 深度学习人工智能实践应用 Linux Ubuntu 中 安装Tensroflow 与 Keras
    卡尔曼滤波的总结
    MATLAB在一张图上画出多条曲线
    数据库的索引和优化
    线程进程
    static关键字
    单例模式
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3444130.html
Copyright © 2011-2022 走看看