为什么派生类中的虚拟析构函数是空的? [英] Why the virtual destructor in derived class is empty?

查看:155
本文介绍了为什么派生类中的虚拟析构函数是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我看到在一些代码,虚拟析构函数在派生类是空的,那么为什么我们需要它,因为它什么也没有?它是用来调用基类中的析构函数,没有它,析构函数不能被调用?或者它只是一个符号告诉代码阅读器这个析构函数是虚拟的,这使得代码更容易阅读?谢谢!

I have one question, I see that in some codes, the virtual destructor in derived class is empty, then why we need it since it does nothing? Is it used to call the destructor in base class and without it, the destructor cannot be called? or it is just a notation to tell the code reader that this destructor is virtual which makes code much easier to read? Thanks!

推荐答案


为什么我们需要它,因为它什么都不做?

why we need it since it does nothing?

如果基类已经有一个虚拟析构函数,我们不必在派生类中定义一个空的。放置它的唯一原因是提醒其他读者你的代码有一个虚拟的析构函数类。

If the base class already has a virtual destructor, we don't have to define an empty one in the derived class. The only reason to put it there is to remind other readers of your code that there is a virtual destructor for the class.


在基类中调用析构函数,没有它,析构函数不能被调用。

Is it used to call the destructor in base class and without it, the destructor cannot be called?

一个空的析构函数参与析构函数链与非空或继承析构函数的方式相同。只要基类有一个虚拟析构函数,提供一个空的覆盖不会改变行为。

An empty destructor does participate in destructor chaining in the same way as a non-empty or an inherited destructor does. As long as the base class has a virtual destructor, providing an empty override does not change the behavior.

这篇关于为什么派生类中的虚拟析构函数是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆