C ++类型铸造 [英] C++ type casting

查看:111
本文介绍了C ++类型铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Possible Duplicate:
When should static_cast, dynamic_cast and reinterpret_cast be used?

直到几天前,我一直在C ++中使用C风格类型转换,因为它似乎工作得很好。我最近发现在C ++中使用C非常糟糕。

Until a few days ago, I've always used C style type casting in C++ because it seemed to work good. I recently found out that using C in C++ is very bad..

我从来没有真正使用过C ++的转换,所以我想知道是否有人可以告诉我用他们自己的话最好)static_cast,reinterpret_cast和const_cast之间的区别是什么?

I've never really used C++ casting before, so I'm wondering if someone could tell me (in their own words preferably) what the difference between static_cast, reinterpret_cast and const_cast are?

const_cast我知道从某事中删除一个const,但我不知道什么

const_cast I know removes a "const" from something, but I'm not sure what the difference between them all is, and what one I need to use in different situations.

推荐答案

说C casting is bad 是一个极端,它本身就像使用C风格的casts一样糟糕。

To say "C casting is bad" is an extremity that by itself is about as bad as using C-style casts all the time.

应该使用新C ++风格的转换的区域是:分层转换(upcasts,downcasts,crosscasts),const-正确性转换和重新解释转换。对于算术铸造C风格的转换工作完全精细,没有危险,这就是为什么他们可以安全地使用在C ++代码。事实上,我实际上建议使用C型铸造作为算术铸造 - 只是为了使算术铸造看起来不同于其他铸造类型。

The areas where "new" C++ style casts should be used are: hierarchical casts (upcasts, downcasts, crosscasts), const-correctness casts and reinterpretation casts. For arithmetical casts C-style casts work perfectly fine and pose no danger, which is why they can safely be used in C++ code. In fact, I would actually recommend using specifically C-style casts as arithmetical casts - just to make arithmetical casts to look different from other cast types.

这篇关于C ++类型铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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