Virtual Destructor
来源:互联网 发布:钢琴演奏软件 编辑:程序博客网 时间:2024/06/06 13:10
Base *b = new Derived();// use bdelete b; // Here's the problem!
如果析构函数不是虚函数,则运行哪个析构函数是由该指针的类型决定的。如上例,若类Base的析构函数不是虚函数,而b指向的类型是派生类型的,此时delete b是undefined behaviour。
若果析构函数是虚函数,则运行哪个析构函数由该指针指向的对象的类型决定。如上例,若类Base的析构函数是虚函数,则delete b则调用Derived的析构函数。
一、http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/
Example without a Virtual Destructor:
#include iostream.hclass Base{ public: Base(){ cout<<"Constructing Base";} // this is a destructor:~Base(){ cout<<"Destroying Base";}};class Derive: public Base{ public: Derive(){ cout<<"Constructing Derive";} ~Derive(){ cout<<"Destroying Derive";} };void main(){ Base *basePtr = new Derive(); delete basePtr;}
The output after running the code above would be:
Constructing Base Constructing Derive Destroying Base
Based on the output above, we can see that the constructors get called in the appropriate order when we create the Derive class object pointer in the main function.
But there is a major problem with the code above: the destructor for the "Derive" class does not get called at all when we delete ‘basePtr’.
So, how can we fix this problem?
Well, what we can do is make the base class destructor virtual, and that will ensure that the destructor for any class that derives from Base (in our case, its the "Derive" class) will be called.
Example with a Virtual Destructor:
So, the only thing we will need to change is the destructor in the Base class and here’s what it will look like – note that we highlighted the part of the code where the virtual keyword has been added in red:
class Base{ public: Base(){ cout<<"Constructing Base";}// this is a virtual destructor:virtual ~Base(){ cout<<"Destroying Base";}};
Now, with that change, the output after running the code above will be:
Constructing Base Constructing Derive Destroying DeriveDestroying Base
Note that the derived class destructor will be called before the base class.
So, now you’ve seen why we need virtual destructors and also how they work.
One important design paradigm of class design is that if a class has one or more virtual functions, then that class should also have a virtual destructor.
二、http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors
Virtual destructors are useful when you can delete an instance of a derived class through a pointer to base class:
class Base { // some virtual methods};class Derived : public Base{ ~Derived() { // Do some important cleanup }}Here, you'll notice that I didn't declare Base's destructor to be virtual. Now, let's have a look at the following snippet:
Base *b = new Derived();// use bdelete b; // Here's the problem!
Since Base's destructor is not virtual and b
is a Base*
pointing to a Derived
object, delete b
has undefined behaviour. In most implementations, the call to the destructor will be resolved like any non-virtual code, meaning that the destructor of the base class will be called but not the one of the derived class, resulting in resources leak.
- virtual destructor
- Virtual Destructor
- Virtual Destructor
- 虚析构函数 virtual destructor
- smart pointer virtual destructor
- C++ virtual destructor
- virtual destructor的使用
- virtual destructor问题
- C++ virtual destructor
- C++中的 virtual destructor
- What is a virtual destructor?
- When to use virtual destructor?
- !!!Abstract class & virtual destructor problems
- non-virtual destructor错误解决
- Pure virtual destructor in C++
- virtual method but non-virtual destructor
- 永远让基类拥有virtual的destructor
- virtual destructor是干什么用的
- 自己换了空调滤芯,终于告别了泥土味
- Java获取下个月的最后一天
- 如何在OTN网站快速找到asm包并下载 (Oracle RAC)
- 购买水烟壶
- ios申请真机调试( xcode 5)详细解析
- Virtual Destructor
- 动态内存分配
- nyoj ASCII码排序
- linux进程并发模型生产者和消费者模式编程
- 九九乘法表
- 鸡兔同笼
- nyoj 画图
- nyoj- 成绩转换
- nyoj-大小写互换