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
}
};
Here, you'll notice that I didn't declare Base's destructor to be virtual
.
(在这里,您会注意到我没有将Base的析构函数声明为virtual
。)
Now, let's have a look at the following snippet: (现在,让我们看一下以下代码片段:)
Base *b = new Derived();
// use b
delete 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 :
(由于Base的析构函数不是virtual
并且b
是指向Derived
对象的Base*
,因此delete b
具有未定义的行为 :)
[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 .
([在delete b
],如果要删除的对象的静态类型与其动态类型不同,则静态类型应为要删除的对象的动态类型的基类,并且静态类型应具有虚拟析构函数或行为未定义 。)
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.
(综上所述,在需要对基类的析构函数进行多态操作时,始终使它们virtual
。)
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. (这样,编译器将不允许您在基类指针上调用delete
。)
You can learn more about virtuality and virtual base class destructor in this article from Herb Sutter .
(您可以从Herb Sutter的本文中了解有关虚拟性和虚拟基类析构函数的更多信息。)