演员的两个含义 [英] two meanings of a cast

查看:86
本文介绍了演员的两个含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多年来我一直没有考虑过这个问题,但是最近我偶然发现了''cast''实际上正在做(至少)两个不同的事实。 >
东西:


一方面''cast''意味着:''将某些东西变成某种东西

else''。示例:double d = 9.99; long l =(double)d;


另一方面''cast''表示:''将某事物视为某物

其他没有改变'' 。例如,在投射函数指针时。


你同意吗?


IMO转换和处理之间的区别对于C新人而言,并不是很可能已经指出了足够多的b $ b可能是造成危险的一个原因。

Felix

解决方案

Felix Kater写道:


我没想过它已经好几年了,但最近我偶然发现

''cast''实际上正在做(至少)两种不同的b
事物:


一方面''cast''意味着:''将某些东西变成某种东西

else''。示例:double d = 9.99; long l =(double)d;


另一方面''cast''表示:''将某事物视为某物

其他没有改变'' 。例如,在投射函数指针时。


你同意吗?



No.


在演员表达式`(T)E`中,我们要求的值是表达

`E`使用适当的转换机制转换为类型`T`。


各地都是如此。


有时,适当的转换机制使基础

位模式保持不变,导致这种错觉,即演员意味着将某些东西视为没有变化的东西。但这是一个意外的实施。


[我们在过去看到过从`char *`转换为`int *`和<在某些实现中,
返回需要有点代码。]


IMO转换和处理之间的这种区别是可能

足以指出C新人,可能是造成危险的原因之一。



我不认为这么简单。我怀疑危险来自

指针的投射以及如果`p`指向某处理智的信念那么

指针`(T *)p`指向同样理智的地方并且是dereferencable

来获取类型`T`的值。


-

尼特采摘最好在朋友之间进行。


Hewlett-Packard Limited Cain Road,Bracknell,注册号:

注册办事处:Berks RG12 1HN 690597 England


>>>>" FK" == Felix Kater< fk **** @ googlemail.comwrites:


FKOn一方面''cast''意味着:''将某些内容改为

FKsomthing''。示例:double d = 9.99; long l =(double)d;


FKOn另一方面''cast''意味着:''处理某事物为

FKs其他没有变化'' 。例如,当你施放

FKfunction指针时。


FK你同意吗?


否;在你的第一个案例中,你也将某事物视为某种东西而不是改变。当你把它变成双倍时,d的值不会改变(完全没必要,因为那就是它)和

将它分配给l。 br $>
Charlton


-

Charlton Wilbur
cw ***** @ chromatico.net


4月27日下午1:23,Chris Dollin< ; chris.dol ... @ hp.comwrote:


< snip>


有时,适当转换机械使基础

位模式保持不变,导致这种错觉,即演员意味着将某些东西视为没有变化的东西。但这是实施

的意外。



< snip>


你在这里说有一个实现会

改变对象的位模式,因为它是[保持

原始大小不变] - 或者位的变化可能是

结果改变原件尺寸?


I haven''t been thinking about it for years but recently I''ve stumbled on
the fact that ''casting'' is actually doing (at least) two different
things:

On the one hand ''casting'' means: ''Change something into somthing
else''. Example: double d=9.99; long l=(double)d;

On the other hand ''casting'' means: ''Treating something as something
else without change''. For instance when casting function pointers.

Would You agree?

IMO this difference between ''conversion'' and ''treatment'' isn''t probably
pointed out enough to C newcomers and may be one reason for the danger
of casts.

Felix

解决方案

Felix Kater wrote:

I haven''t been thinking about it for years but recently I''ve stumbled on
the fact that ''casting'' is actually doing (at least) two different
things:

On the one hand ''casting'' means: ''Change something into somthing
else''. Example: double d=9.99; long l=(double)d;

On the other hand ''casting'' means: ''Treating something as something
else without change''. For instance when casting function pointers.

Would You agree?

No.

In a cast expression `(T) E`, we''re requiring the value of the expression
`E` to be converted to type `T` using the appropriate conversion machinery.

That''s true everywhere.

Sometimes, the "appropriate conversion machinery" leaves the underlying
bit-pattern unchanged, leading to the illusion that the cast means "Treat
something as something else without change". But this is an accident of
implementation.

[We have seen in the past that conversion from say `char*` to `int*` and
back requires bit-fiddling code in some implementations.]

IMO this difference between ''conversion'' and ''treatment'' isn''t probably
pointed out enough to C newcomers and may be one reason for the danger
of casts.

I don''t think it''s as simple as that. I suspect the danger comes from
casts of pointers and a belief that if `p` points somewhere sane then
the pointer `(T*) p` points somewhere equally sane and is dereferencable
to get values of type `T`.

--
Nit-picking is best done among friends.

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England


>>>>"FK" == Felix Kater <fk****@googlemail.comwrites:

FKOn the one hand ''casting'' means: ''Change something into
FKsomthing else''. Example: double d=9.99; long l=(double)d;

FKOn the other hand ''casting'' means: ''Treating something as
FKsomething else without change''. For instance when casting
FKfunction pointers.

FKWould You agree?

No; in your first case, you''re also "treating something as something
else without change." The value of d does not change when you cast it
to a double (completely unnecessary, since that''s what it is) and
assign it to l.

Charlton

--
Charlton Wilbur
cw*****@chromatico.net


On Apr 27, 1:23 pm, Chris Dollin <chris.dol...@hp.comwrote:

<snip>

Sometimes, the "appropriate conversion machinery" leaves the underlying
bit-pattern unchanged, leading to the illusion that the cast means "Treat
something as something else without change". But this is an accident of
implementation.

<snip>

Are you saying here that there exists an implementation that would
alter the bit pattern of an object as it is cast [keeping the
original''s size constant] - or that the change in bits would likely be
the result of changing the size of the original?


这篇关于演员的两个含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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