static_cast< int>(foo)vs.(int)foo [英] static_cast<int>(foo) vs. (int)foo

查看:121
本文介绍了static_cast< int>(foo)vs.(int)foo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

不同的是,(int)foo可能意味着六个不同的东西。
它可能是一个static_cast(在静态已知类型之间转换),它可能是一个const_cast(添加或删除常量),或者可能是一个reinterpret_cast(在指针类型之间转换)



编译器尝试每一个,直到找到一个工作。这意味着它可能不会总是选择你期望的,所以它可以成为一个微妙的错误来源。



此外,static_cast更容易搜索或做搜索/替换。


Could somebody please elaborate on the differences?

解决方案

The difference is that (int)foo can mean half a dozen different things. It might be a static_cast (convert between statically known types), it might be a const_cast (adding or removing const-ness), or it might be a reinterpret_cast (converting between pointer types)

The compiler tries each of them until it finds one that works. Which means that it may not always pick the one you expect, so it can become a subtle source of bugs.

Further, static_cast is a lot easier to search for or do search/replace on.

这篇关于static_cast< int>(foo)vs.(int)foo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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