虚拟析构函数是继承的吗?

2021-12-17 00:00:00 inheritance c++ virtual-destructor

如果我有一个带有虚拟析构函数的基类.是否有派生类也要声明虚析构函数?

If I have a base class with a virtual destructor. Has a derived class to declare a virtual destructor too?

class base {
public:
    virtual ~base () {}
};

class derived : base {
public:
    virtual ~derived () {} // 1)
    ~derived () {}  // 2)
};

具体问题:

  1. 1) 和 2) 一样吗?2) 是因为它的基础而自动虚拟化还是停止"虚拟化?
  2. 派生的析构函数如果无关,可以省略吗?
  3. 声明派生析构函数的最佳做法是什么?声明它是虚拟的、非虚拟的还是尽可能省略它?

推荐答案

  1. 是的,它们是一样的.派生类没有声明虚拟的东西并不会阻止它成为虚拟的.事实上,如果任何方法(包括析构函数)在派生类中是虚拟的,则无法阻止它在基类中是虚拟的.在 >=C++11 中,您可以使用 final 来防止它在派生类中被覆盖,但这并不能防止它成为虚拟的.
  2. 是的,派生类中的析构函数可以省略,如果它无关.并且它是否是虚拟的并不重要.
  3. 如果可能,我会省略它.为了清楚起见,我总是对派生类中的虚函数使用 virtual 关键字或 override.人们不应该一直向上继承层次结构来确定函数是虚拟的.此外,如果您的类是可复制或可移动的,而无需声明您自己的复制或移动构造函数,则声明任何类型的析构函数(即使您将其定义为 default)将迫使您声明副本和如果需要,请移动构造函数和赋值运算符,因为编译器将不再为您放置它们.
  1. Yes, they are the same. The derived class not declaring something virtual does not stop it from being virtual. There is, in fact, no way to stop any method (destructor included) from being virtual in a derived class if it was virtual in a base class. In >=C++11 you can use final to prevent it from being overridden in derived classes, but that doesn't prevent it from being virtual.
  2. Yes, a destructor in a derived class can be omitted if it has nothing to do. And it doesn't matter whether or not its virtual.
  3. I would omit it if possible. And I always use either the virtual keyword or override for virtual functions in derived classes for reasons of clarity. People shouldn't have to go all the way up the inheritance hierarchy to figure out that a function is virtual. Additionally, if your class is copyable or movable without having to declare your own copy or move constructors, declaring a destructor of any kind (even if you define it as default) will force you to declare the copy and move constructors and assignment operators if you want them as the compiler will no longer put them in for you.

作为第 3 条的一个小点.在评论中已经指出,如果析构函数未声明,编译器会生成一个默认的(仍然是虚拟的).并且默认的是一个内联函数.

As a small point for item 3. It has been pointed out in comments that if a destructor is undeclared the compiler generates a default one (that is still virtual). And that default one is an inline function.

内联函数可能会将程序的更多部分暴露给程序其他部分的更改,并使共享库的二进制兼容性变得棘手.此外,增加的耦合会导致在面对某些类型的更改时进行大量重新编译.例如,如果你决定你真的想要一个虚拟析构函数的实现,那么调用它的每一段代码都需要重新编译.而如果您已在类主体中声明它,然后在 .cpp 文件中将其定义为空,则无需重新编译就可以更改它.

Inline functions potentially expose more of your program to changes in other parts of your program and make binary compatibility for shared libraries tricky. Also, the increased coupling can result in a lot of recompilation in the face of certain kinds of changes. For example, if you decide you really do want an implementation for your virtual destructor then every piece of code that called it will need to be recompiled. Whereas if you had declared it in the class body and then defined it empty in a .cpp file you would be fine changing it without recompiling.

我个人的选择仍然是尽可能省略它.在我看来,它会使代码变得混乱,并且编译器有时可以使用默认实现而不是空实现来做更高效的事情.但是,您可能会受到一些限制,使之成为一个糟糕的选择.

My personal choice would still be to omit it when possible. In my opinion it clutters up the code, and the compiler can sometimes do slightly more efficient things with a default implementation over an empty one. But there are constraints you may be under that make that a poor choice.

相关文章