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