c++ - 什么时候使用虚拟析构函数?

标签 c++ polymorphism shared-ptr virtual-destructor

我对大多数 OOP 理论都有扎实的理解,但让我很困惑的一件事是虚拟析构函数。

我认为对于链中的每个对象,无论如何总是会调用析构函数。

您打算什么时候将它们虚拟化?为什么?

最佳答案

当您可能通过指向基类的指针删除派生类的实例时,虚拟析构函数很有用:

class Base 
{
    // some virtual methods
};

class Derived : public Base
{
    ~Derived()
    {
        // Do some important cleanup
    }
};

在这里,您会注意到我没有将 Base 的析构函数声明为 virtual。现在,让我们看一下以下片段:

Base *b = new Derived();
// use b
delete b; // Here's the problem!

因为 Base 的析构函数不是 virtual 并且 b 是一个指向 Derived 对象的 Base*delete bundefined behaviour :

[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.

在大多数实现中,对析构函数的调用将像任何非虚拟代码一样被解析,这意味着将调用基类的析构函数而不是派生类的析构函数,从而导致资源泄漏。

总而言之,当基类的析构函数要进行多态操作时,始终将它们设为虚拟

如果要防止通过基类指针删除实例,可以将基类析构函数设置为protected和nonvirtual;这样一来,编译器将不允许您在基类指针上调用 delete

您可以在 this article from Herb Sutter 中了解有关虚拟性和虚拟基类析构函数的更多信息.

关于c++ - 什么时候使用虚拟析构函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539063/

相关文章:

c++ - 如何使用 std::rel_ops 自动提供比较运算符?

c++ - 将方法传递给方法并替换(几乎完全相同的)重复代码

C++ 样式 : copyable handle wrapper classes

c++ - 基础接口(interface)的 shared_ptr 的初始化和管理

c++ - cout 上抽象类的多态性

C++11 的 shared_ptr 赋值

c++ - 为什么有些运算符只能作为成员函数重载,其他的作为友元函数重载,而其余的则两者都重载?

c++ - 使用交换函数交换 vector 和数组中两行的复杂性

c++ - C++、Windows 中的简单 SSL 套接字

c++ - 静态基类型和动态派生类型的输出