虚拟析构函数被重新审视 [英] virtual destructor revisted

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

问题描述

声明虚拟析构函数的原因。对于Base类来说,当使用Base

类型的指针删除Derived的对象时,确保将调用Derived类的析构函数。


这是定义虚拟析构函数的唯一原因吗?对于基类?


因为任何Base类的用户总是可以删除这样的

派生对象,这是否意味着虚拟析构器是否需要



提前致谢!


A reason for declaring a "virtual destructor" for a Base class is to make
sure the destructor of Derived class will be invoked when a pointer of Base
type is used to delete an object of Derived.

Is this the only reason to define "virtual destructor" for a Base class?

Since it''s always possible that users of any Base class will delete a
Derived object like that, does this mean that a "virtual destructor" is
always necessary?

Thanks in advance!


推荐答案

ctick写道:
ctick wrote:
声明虚拟析构函数的原因对于Base类来说,当
Base类型的指针用于删除Derived的对象时,确保将调用Derived类的析构函数。

这是唯一的理由是定义虚拟析构函数对于基类?

因为任何Base类的用户总是可以删除这样的衍生对象,这是否意味着虚拟析构函数是否总是有必要?

提前致谢!
A reason for declaring a "virtual destructor" for a Base class is to make
sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived.

Is this the only reason to define "virtual destructor" for a Base class?

Since it''s always possible that users of any Base class will delete a
Derived object like that, does this mean that a "virtual destructor" is
always necessary?

Thanks in advance!




将所有析构函数设为虚拟,除非分析显示你真的真的

真的不需要一个。


如果你的类是抽象的,只有纯虚方法,那么

析构函数也是纯虚拟的。


只有一个事件序列导致未定义的行为:基类没有

虚拟析构函数,你从中派生出来,创建一个''new' '派生对象,取

这个对象的地址,将地址转换为基本类型,然后''删除''。


''删除基本类型指针并不是一种罕见的事件序列,而是多种多样的设计。你应该更加小心,确保''new''的返回值总是存储在一个智能指针或等价的

正确的派生类型中,所以破坏变得自动化。而且很多

多态设计只使用堆栈来存储对象,因此所有这些

情况都会破坏并正确清理。


但是,重构和新功能可以轻松地将静态的

分配对象迁移到动态分配对象中。此时,系统中没有任何内容

会告诉您删除是否会创建未定义的行为(除了可能是一个

lint工具)。


语言允许这个漏洞允许调整性能。有时

虚拟方法表的微小开销会受到伤害,所以你总是可以把它拿出来。

out。一个名为unvirtual的关键字会很傻,所以你必须总是

让析构者虚拟化。


-

Phlip
http://industrialxp.org/community/bi...UserInterfaces




" ctick" <克拉*** @ flare.com>在消息中写道

新闻:bZ ***************** @ bgtnsc05-news.ops.worldnet.att.net ...

"ctick" <ct***@flare.com> wrote in message
news:bZ*****************@bgtnsc05-news.ops.worldnet.att.net...
声明虚拟析构函数的原因。对于Base类来说,当
Base类型的指针用于删除Derived的对象时,确保将调用Derived类的析构函数。

这是唯一的理由是定义虚拟析构函数对于基类?


非常好。还有一个相当模糊的原因。可能是你希望类Base至少拥有一个虚函数(例如你可以在
上使用dynamic_cast),但是你没有任何其他合适的

方法声明为虚拟。

因为任何Base类的用户总是可以删除这样的衍生对象,这是否意味着虚拟析构器是指虚拟析构器。总是有必要吗?
A reason for declaring a "virtual destructor" for a Base class is to make
sure the destructor of Derived class will be invoked when a pointer of Base type is used to delete an object of Derived.

Is this the only reason to define "virtual destructor" for a Base class?
Pretty much yes. There is one other rather obscure reason. It might be that
you want class Base to have at least one virtual function (so that you can
use dynamic_cast on it for instance) but you don''t have any other suitable
method to declare as virtual.

Since it''s always possible that users of any Base class will delete a
Derived object like that, does this mean that a "virtual destructor" is
always necessary?




不是真的。使用虚拟功能会产生开销。如果您的班级

不打算以多态方式使用,那么就不要使用任何虚拟的

函数(包括析构函数)并记录该事实。不幸的是,

总是可能会让用户做些蠢事。


john



Not really. There is an overhead to using virtual functions. If your class
is not intended to be used polymorphically then don''t use any virtual
functions (including destructor) and document that fact. Unfortunately its
always possible that users will do something stupid.

john


Phlip写道:
Phlip wrote:
ctick写道:
ctick wrote:
声明虚拟析构函数的原因对于Base类
是为了确保在使用Base类型的指针删除Derived的对象时调用Derived类的析构函数。

这是唯一的理由是定义虚拟析构函数对于基类?

因为任何基类的用户总是可以删除这样的派生对象,这是否意味着虚拟析构函数"总是有必要吗?
A reason for declaring a "virtual destructor" for a Base class
is to make sure the destructor of Derived class will be invoked
when a pointer of Base type is used to delete an object of Derived.

Is this the only reason to define "virtual destructor" for a Base class?

Since it''s always possible that users of any Base class
will delete a Derived object like that, does this mean that
a "virtual destructor" is always necessary?



让所有的析构函数都是虚拟的
除非分析显示你真的真的不需要。



Make all destructors virtual
unless profiling reveals you really really really don''t need one.




这是*非常糟糕的建议。

如果所有的C ++ destuctors都是虚拟的,那么这个特性将被内置到该语言中 -
或该语言将被重命名为Java。



This is *very* bad advice.
If all C++ destuctors were meant to be virtual,
that feature would have been built into the language --
or the language would have been renamed Java.


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

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