仅具有虚函数的类的虚拟析构函数? [英] virtual destructors for classes only with virtual functions?

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

问题描述




大多数关于C ++的书都说这样:如果类包含至少一个虚拟析构函数

一个虚拟会员

功能。


我的问题是:为什么只有当班级包含在
$ b $的情况下至少一个虚拟成员函数?难道每当有可能继承对象将通过基类指针销毁时,析构函数是否永远都不是虚拟的?b
$ b (这不需要,

基类有任何其他虚函数!)


谢谢!


ps:有人可以简单解释一下,为什么一个具体的基类

不能有一个抽象派生类?

Hi,

Most of the books on C++ say something like this: "A virtual destructor
should be defined if the class contains at least one virtual member
function."

My question is: why is it only for the case when the class contains at
least one virtual member function? Shouldn''t the destructor always be
virtual, whenever there''s a possibility that an inherited object will
be destructed through a base class pointer? (This does not require,
that the base class have any other virtual function!)

Thanks!

ps: can someone please explain me briefly, why a concrete base class
cannot have an abstract derived class?

推荐答案

heted7写道:
heted7 wrote:
我的问题是:为什么只有在类包含至少一个虚拟成员函数的情况下?难道每当有可能通过基类指针破坏继承的对象时,析构函数是否总是虚拟的? (这不需要,基类具有任何其他虚函数!)
My question is: why is it only for the case when the class contains at
least one virtual member function? Shouldn''t the destructor always be
virtual, whenever there''s a possibility that an inherited object will
be destructed through a base class pointer? (This does not require,
that the base class have any other virtual function!)




因为你想要破坏polimorphically
不使用polimorphically的对象,大部分书籍

讨论更常见的情况。在这种情况下将其添加到课堂上有一些

的开销,很多人不想支付这个价格。


但是如果你有理由要加一个虚拟的析构函数,做到这一点。这些书说

,在这种情况下你必须添加它,而不是你不能在任何其他

的情况下这样做。


-

Salu2



Because it will be uncommon that you wants to destroy polimorphically
objetcts that are not used polimorphically, and the majority of the books
talks about more common cases. And to add it to class in this case has some
overhead and many people does not want to pay this price.

But if you have reasons to add a virtual destructor, do it. The books says
that in that case you must add it, not that you must not do it in any other
case.

--
Salu2


JuliánAlbo写道:
Julián Albo wrote:

但如果你有理由添加一个虚拟析构函数,做到这一点。书中说
在这种情况下你必须添加它,而不是你不能在任何其他情况下这样做。

But if you have reasons to add a virtual destructor, do it. The books says
that in that case you must add it, not that you must not do it in any other
case.




And,就像没有说其他情况你需要它是一个

过度简化,说当你有虚拟时总是需要它。
函数过于简单化。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


谢谢你们两位!

Thanks for both of you!


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

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