简体   繁体   English

为什么派生类中的虚析构函数为空?

[英]Why the virtual destructor in derived class is empty?

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. 只要基类具有虚拟析构函数,提供空覆盖不会改变行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当派生类的析构函数是非虚拟的时,为什么基类析构函数在派生对象上调用? - Why is base-class destructor called on derived object when destructor of derived class is non-virtual? 具有虚析构函数的派生类的sizeof - Sizeof of derived class with virtual destructor 如果基本 class 析构函数是虚拟的,则派生 class 的析构函数是否默认为虚拟? - Is the destructor of a derived class virtual by-default if the base class destructor is virtual? 为什么派生类析构函数被调用,即使基类析构函数不是虚拟的,如果将对象创建为引用 - Why derived class destructor called even though base class destructor is not virtual if object is created as reference 为什么派生类的析构函数被调用? - Why is the destructor of the derived class called? 虚拟析构函数在基类和派生类中未调用 - Virtual Destructor Not called in Base as well as Derived Class 是否适合在派生类析构函数中调用虚函数? - Is appropriate to call virtual function in derived class destructor? 大多数派生类的析构函数中的纯虚拟调用 - Pure virtual call in destructor of most derived class 如果基类析构函数是虚拟的,是否需要派生类析构函数定义? - Is a derived class destructor definition required if base class destructor is virtual? C ++派生类和虚拟析构函数 - C++ derived class and virtual destructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM