使用 C++ 样式转换将 int 转换为 char

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16899055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:44:58  来源:igfitidea点击:

casting int to char using C++ style casting

c++castingcharint

提问by Subway

In traditional C you can do:

在传统 C 中,您可以执行以下操作:

int i = 48;
char c = (char)i;
//Now c holds the value of 48. 
//(Of course if i > 255 then c will not hold the same value as i).  

Which of the c++ casting methods (static_cast, reinterpret_cast) is suited for getting this job done?

哪种 C++ 转换方法(static_cast、reinterpret_cast)适合完成这项工作?

采纳答案by Mike Seymour

You can implicitly convert between numerical types, even when that loses precision:

您可以在数值类型之间进行隐式转换,即使会丢失精度:

char c = i;

However, you might like to enable compiler warnings to avoid potentially lossy conversions like this. If you do, then use static_castfor the conversion.

但是,您可能希望启用编译器警告以避免这样的潜在有损转换。如果这样做,则static_cast用于转换。

Of the other casts:

在其他演员阵容中:

  • dynamic_castonly works for pointers or references to polymorphic class types;
  • const_castcan't change types, only constor volatilequalifiers;
  • reinterpret_castis for special circumstances, converting between pointers or references and completely unrelated types. Specifically, it won't do numeric conversions.
  • C-style and function-style casts do whatever combination of static_cast, const_castand reinterpret_castis needed to get the job done.
  • dynamic_cast仅适用于对多态类类型的指针或引用;
  • const_cast不能改变类型,只有constvolatile限定符;
  • reinterpret_cast用于特殊情况,在指针或引用和完全不相关的类型之间进行转换。具体来说,它不会进行数字转换。
  • C 风格和函数风格的强制转换可以做 , 的任何组合static_castconst_cast并且reinterpret_cast是完成工作所必需的。

回答by Snps

You should use static_cast<char>(i)to cast the integer ito char.

您应该使用static_cast<char>(i)将整数转换ichar.

reinterpret_castshould almost never be used, unless you want to cast one type into a fundamentally different type.

reinterpret_cast几乎不应该使用,除非您想将一种类型转换为根本不同的类型。

Also reinterpret_castis machine dependent so safely using it requires complete understanding of the types as well as how the compiler implements the cast.

reinterpret_cast依赖于机器,所以安全地使用它需要完全理解类型以及编译器如何实现强制转换。

For more information about C++ casting see:

有关 C++ 转换的更多信息,请参见:

回答by Sanish

reinterpret_castcannot be used for this conversion, the code will not compile. According to C++03 standard section 5.2.10-1:

reinterpret_cast不能用于此转换,代码将无法编译。根据 C++03 标准第 5.2.10-1 节:

Conversions that can be performed explicitly using reinterpret_cast are listed below. No other conversion can be performed explicitly using reinterpret_cast.

Conversions that can be performed explicitly using reinterpret_cast are listed below. No other conversion can be performed explicitly using reinterpret_cast.

This conversion is not listed in that section. Even this is invalid:

此转换未在该部分中列出。即使这是无效的:

long l = reinterpret_cast<long>(i)

static_castis the one which has to be used here. See thisand thisSO questions.

static_cast是必须在这里使用的那个。看到这个这个SO 问题。

回答by guest

Using static cast would probably result in something like this:

使用静态转换可能会导致如下结果:

// This does not prevent a possible type overflow
const char char_max = -1;

int i = 48;
char c = (i & char_max);

To prevent possible type overflow you could do this:

为了防止可能的类型溢出,您可以这样做:

const char char_max = (char)(((unsigned char) char(-1)) / 2);

int i = 128;
char c = (i & char_max); // Would always result in positive signed values.

Where reinterpret_cast would probably just directly convert to char, without any cast safety. -> Never use reinterpret_cast if you can also use static_cast. If you're casting between classes, static_cast will also ensure, that the two types are matching (the object is a derivate of the cast type).

哪里 reinterpret_cast 可能只是直接转换为 char,没有任何转换安全。-> 如果您也可以使用 static_cast,请不要使用 reinterpret_cast。如果您在类之间进行转换,static_cast 还将确保两种类型匹配(对象是转换类型的派生类)。

If your object a polymorphic type and you don't know which one it is, you should use dynamic_cast which will perform a type check at runtime and return nullptr if the types do not match.

如果您的对象是多态类型并且您不知道它是哪一种,您应该使用 dynamic_cast,它将在运行时执行类型检查,如果类型不匹配则返回 nullptr。

IF you need const_cast you most likely did something wrong and should think about possible alternatives to fix const correctness in your code.

如果你需要 const_cast 你很可能做错了什么,应该考虑可能的替代方法来修复你的代码中的 const 正确性。