动物广播到底是怎么回事? [英] What the hell is dynamic_cast for?

查看:68
本文介绍了动物广播到底是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下三个类,


GrandBase< - Base< - Child< - GrandChild


以下演员表达式只有在pBase指向对象

类型为``Child'''或'`GrandChild''''时才成立,即类型不高于

上述类层次结构中的子项,

dynamic_cast< Child *> pBase


我是否正确绘制了这个?


向上铸造是安全的,而向下铸造则不安全。 dynamic_cast似乎做了b / b
,所以为什么dynamic_cast打算做危险的

工作?

Suppose I have the following three classes,

GrandBase <-- Base <-- Child <-- GrandChild

The following cast expression holds true only if pBase points object
of type of ``Child'''' or ``GrandChild'''', i.e. types not upper than
Child in the above class hierarchy,

dynamic_cast<Child*>pBase

Do I draw this correctly?

Up-casting is safe while down-casting is not. dynamic_cast seems to do
down-casting, so why is dynamic_cast intenting to do the dangerous
work?

推荐答案

lovecreatesbea...@gmail.com写道:
lovecreatesbea...@gmail.com wrote:

向上投射是安全的,而向下投射则不是。 dynamic_cast似乎做了b / b
,所以为什么dynamic_cast打算做危险的

工作?
Up-casting is safe while down-casting is not. dynamic_cast seems to do
down-casting, so why is dynamic_cast intenting to do the dangerous
work?



动态播放旨在执行危险工作。更安全。

虽然static_cast只是转换为派生类型而不管

对象是否属于该类型,但动态转换实际上会检查

对象属于该类型,如果不是,则返回空指针。

您可以检查返回的指针以查看转换是否成功。

(使用static_cast,你可能只会遇到分段错误。)

有可能无法避免向下倾斜或者阻力最小的路径

。因此,有一个工具让它更安全是件好事。

Dynamic casting is intended to do the "dangerous work" more safely.
While a static_cast will simply cast to the derived type regardless of
whether the object is of that type, dynamic cast actually checks that
the object is of that type, and if it isn''t, it returns a null pointer.
You can check the returned pointer to see if the cast was successful.
(With static_cast you probably just get a segmentation fault.)

There are situation where downcasting may be unavoidable or the path
of least resistance. Thus it''s good to have a tool to make it safer.


lovecreatesbea ... @ gmail.com写道:
lovecreatesbea...@gmail.com wrote:

假设我有以下三个班级,


GrandBase< - Base< - Child< - GrandChild


以下强制转换表达式仅在pBase指向对象

类型为``Child'''或'`GrandChild''''时才成立,即类型不高于

上述类层次结构中的子项,

dynamic_cast< Child *> pBase


我是否正确绘制了这个?
Suppose I have the following three classes,

GrandBase <-- Base <-- Child <-- GrandChild

The following cast expression holds true only if pBase points object
of type of ``Child'''' or ``GrandChild'''', i.e. types not upper than
Child in the above class hierarchy,

dynamic_cast<Child*>pBase

Do I draw this correctly?



你错过了括号,你的班级必须是多态的(至少有一个虚拟函数是
)。那是第一次。第二,表达


dynamic_cast< Child *>(pBase)


的类型为''Child *'',而不是如果演员表失败,它将评估

到(Child *)0。

You missed the parentheses and your class has to be polymorphic (have at
least one virtual function). That''s first. And second, the expression

dynamic_cast<Child*>(pBase)

has the type ''Child*'', and not "holds true", although it will evaluate
to (Child*)0 if the cast fails.


\\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n> \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n> \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ n \\ b> \\ n而不是向下铸造。 dynamic_cast似乎做了b / b
,所以为什么dynamic_cast打算做危险的

工作?
Up-casting is safe while down-casting is not. dynamic_cast seems to do
down-casting, so why is dynamic_cast intenting to do the dangerous
work?



不知道你在这里问什么。 ''dynamic_cast''允许*失败*

并且您可以捕获''std :: bad_cast''异常或检查

结果(对于指针result是一个空指针,用于引用

异常抛出)。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Not sure what you''re asking here. ''dynamic_cast'' is allowed to *fail*
and you can either catch the ''std::bad_cast'' exception or check the
result (for pointers the result is a null pointer, for references the
exception is thrown).

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


5月28日下午12:48,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:
On May 28, 12:48 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

lovecreatesbea .. 。@ gmail.com写道:
lovecreatesbea...@gmail.com wrote:

假设我有以下三个类,
Suppose I have the following three classes,


GrandBase< - Base< - Child< - GrandChild
GrandBase <-- Base <-- Child <-- GrandChild


以下演员表达式仅在pBase指向对象时才成立/>
类型的``Child'' '或'`GrandChild'''',即类型不高于

上述类层次结构中的子项,
The following cast expression holds true only if pBase points object
of type of ``Child'''' or ``GrandChild'''', i.e. types not upper than
Child in the above class hierarchy,


dynamic_cast< Child *> pBase
dynamic_cast<Child*>pBase


我是否正确绘制了这个?
Do I draw this correctly?



你错过了括号,你的班级必须是多态的(至少有一个虚拟函数)。


You missed the parentheses and your class has to be polymorphic (have at
least one virtual function).



那不是:有一个虚拟的析构函数?

Wouldn''t that be: "have a vritual destructor"?


这篇关于动物广播到底是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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