(int)ch与int(ch):他们是不同的语法同一件事吗? [英] (int) ch vs. int(ch): Are they different syntaxes for the same thing?

查看:834
本文介绍了(int)ch与int(ch):他们是不同的语法同一件事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,is(int)ch等同于int(ch)。

In C++, is (int) ch equivalent to int(ch).

如果没有,有什么区别?

If not, what's the difference?

推荐答案

同样的事情,也与(int)(ch)相同。在C ++中,通常最好使用命名的方法来澄清您的意图:

They are the same thing, and also the same as (int)(ch). In C++, it's generally preferred to use a named cast to clarify your intentions:


  • 使用 static_cast 在不同大小或签名的原始类型之间进行转换,例如 static_cast< char>(anInteger)

  • 使用 dynamic_cast 基类到派生类(仅限多态类型),例如 dynamic_cast< Derived *>(aBasePtr)

  • 使用 reinterpret_cast 在不同类型的指针之间或在指针和整数之间转换,例如, reinterpret_cast< uintptr_t>(somePtr)

  • 使用 const_cast const volatile 变数的限制符(非常危险),例如 const_cast< char *>(aConstantPointer)

  • Use static_cast to cast between primitive types of different sizes or signednesses, e.g. static_cast<char>(anInteger).
  • Use dynamic_cast to downcast a base class to a derived class (polymorphic types only), e.g. dynamic_cast<Derived *>(aBasePtr).
  • Use reinterpret_cast to cast between pointers of different types or between a pointer and an integer, e.g. reinterpret_cast<uintptr_t>(somePtr).
  • Use const_cast to remove the const or volatile qualifiers from variables (VERY DANGEROUS), e.g. const_cast<char *>(aConstantPointer).

这篇关于(int)ch与int(ch):他们是不同的语法同一件事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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