何时使用虚拟析构函数? [英] When to use virtual destructors?
问题描述
我对大多数 OOP
理论有深刻的理解,但让我很困惑的一件事是虚拟析构函数.
I have a solid understanding of most OOP
theory but the one thing that confuses me a lot is virtual destructors.
我认为无论是什么,对于链中的每个对象,总是会调用析构函数.
I thought that the destructor always gets called no matter what and for every object in the chain.
你打算什么时候让它们虚拟化,为什么?
When are you meant to make them virtual and why?
推荐答案
当您可能通过指向基类的指针删除派生类的实例时,虚拟析构函数很有用:
Virtual destructors are useful when you might potentially 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
}
};
在这里,您会注意到我没有将 Base 的析构函数声明为 virtual
.现在,让我们看看以下代码片段:
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 b
delete b; // Here's the problem!
由于 Base 的析构函数不是 virtual
并且 b
是一个 Base*
指向一个 Derived
对象,delete b
有未定义的行为:
Since Base's destructor is not virtual
and b
is a Base*
pointing to a Derived
object, delete b
has undefined behaviour:
[In delete b
],如果静态类型的要删除的对象与其动态类型不同,静态类型type 应该是对象的动态类型的基类删除并且 静态类型应具有虚拟析构函数或行为未定义.
[In
delete b
], if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.
在大多数实现中,对析构函数的调用将像任何非虚拟代码一样被解析,这意味着将调用基类的析构函数而不是派生类的析构函数,从而导致资源泄漏.
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 a resources leak.
总而言之,当基类的析构函数要进行多态操作时,请始终将它们设为虚拟
.
To sum up, always make base classes' destructors virtual
when they're meant to be manipulated polymorphically.
如果要防止通过基类指针删除实例,可以使基类析构函数为protected和nonvirtual;通过这样做,编译器不会让您在基类指针上调用 delete
.
If you want to prevent the deletion of an instance through a base class pointer, you can make the base class destructor protected and nonvirtual; by doing so, the compiler won't let you call delete
on a base class pointer.
您可以在 Herb Sutter 的这篇文章.
这篇关于何时使用虚拟析构函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!