使用 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
casting int to char using C++ style casting
提问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_cast
for the conversion.
但是,您可能希望启用编译器警告以避免这样的潜在有损转换。如果这样做,则static_cast
用于转换。
Of the other casts:
在其他演员阵容中:
dynamic_cast
only works for pointers or references to polymorphic class types;const_cast
can't change types, onlyconst
orvolatile
qualifiers;reinterpret_cast
is 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_cast
andreinterpret_cast
is needed to get the job done.
dynamic_cast
仅适用于对多态类类型的指针或引用;const_cast
不能改变类型,只有const
或volatile
限定符;reinterpret_cast
用于特殊情况,在指针或引用和完全不相关的类型之间进行转换。具体来说,它不会进行数字转换。- C 风格和函数风格的强制转换可以做 , 的任何组合
static_cast
,const_cast
并且reinterpret_cast
是完成工作所必需的。
回答by Snps
You should use static_cast<char>(i)
to cast the integer i
to char
.
您应该使用static_cast<char>(i)
将整数转换i
为char
.
reinterpret_cast
should almost never be used, unless you want to cast one type into a fundamentally different type.
reinterpret_cast
几乎不应该使用,除非您想将一种类型转换为根本不同的类型。
Also reinterpret_cast
is 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_cast
cannot 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_cast
is the one which has to be used here. See thisand thisSO questions.
回答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 正确性。