如何在 C++ 中使用 reinterpret_cast?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18544849/
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
How to use reinterpret_cast in C++?
提问by sunlight07
I know the reinterpret_castin C++ can be used in this way:
我知道C++ 中的reinterpret_cast可以这样使用:
float a = 0;
int b = *reinterpret_cast<int*>(&a);
But why cannot cast it directly?
但是为什么不能直接投射呢?
float a = 0;
int b = reinterpret_cast<int>(a);
error: invalid cast from type 'float' to type 'int'
采纳答案by Klaim
You can't reinterpret_cast in the case you give because reinterpret_cast takes only either a int to convert to a pointer, or the reverse, and follows additional similar rules.
在你给出的情况下你不能 reinterpret_cast 因为 reinterpret_cast 只需要一个 int 来转换为一个指针,或者相反,并遵循其他类似的规则。
There is a summary of these rules there: http://en.cppreference.com/w/cpp/language/reinterpret_cast
那里有这些规则的摘要:http: //en.cppreference.com/w/cpp/language/reinterpret_cast
回答by Caesar
All reinterpret_cast
does is allow you to read the memory you passed in a different way. You give it a memory location and you ask it to read that memory as if it was what you asked it to. This is why it can only be used with pointers and references.
所有reinterpret_cast
做的就是让你念你以不同的方式通过了记忆。你给它一个内存位置,然后你要求它读取那个内存,就好像它是你要求的一样。这就是为什么它只能与指针和引用一起使用。
Let's take this code as an example:
我们以这段代码为例:
#include <iostream>
int main()
{
float a = 12;
int b = *reinterpret_cast<int*>(&a);
std::cout << b;
}
So to break this line of code into more details *reinterpret_cast<int*>(&a);
:
因此,要将这行代码分解为更多详细信息*reinterpret_cast<int*>(&a);
:
- Take the address of
a
reinterpret_cast
to anint*
- Get back an
int*
that points toa
- Deference the value of the returned pointer as
int
- 取地址
a
reinterpret_cast
到int*
- 取回
int*
指向a
- 将返回的指针的值视为
int
Now when I run this I get 1094713344
, the reason for that is 12 as a float
using IEEE is represented as 0100 0001 0100 0000 0000 0000 0000 0000
in binary. Now take that binary and read it as unsigned int
, then you end up with 1094713344
.
现在,当我运行它时,我得到1094713344
12 的原因,因为float
使用 IEEE0100 0001 0100 0000 0000 0000 0000 0000
以二进制表示。现在获取该二进制文件并将其读取为unsigned int
,然后您最终会得到1094713344
.
This is why reinterpret_cast
is considered to be very dangerous and why it should NOT be used in this type of cases.
这就是为什么它reinterpret_cast
被认为是非常危险的,为什么不应该在这种类型的情况下使用它。
You should only use it when you have a pointer pointing to memory and you need to read that memory in a certain way and you know that the memory can be read in that way.
只有当您有一个指向内存的指针并且您需要以某种方式读取该内存并且您知道可以以这种方式读取该内存时,才应该使用它。
回答by Ivan Aksamentov - Drop
why cannot cast it directly?
为什么不能直接投呢?
I believe that this is a pure design decision, to make C++ more type-safe than C.
我相信这是一个纯粹的设计决定,使C++ 比 C 更类型安全。
reinterpret_cast
is very dangerous, because it can involve type aliasingwhich is a short way to undefined behavior. When you use C++ casts, you sign a contract with your compiler "I know, what I am doing". So, all these long operators names, angle brackets, type-pointer-type conversions telling you: "Wait, don't do it. Maybe there is something wrong in your code design!".
reinterpret_cast
非常危险,因为它可能涉及类型别名,这是未定义行为的一种捷径。当您使用 C++ 强制转换时,您会与编译器签订合同“我知道,我在做什么”。所以,所有这些长运算符名称、尖括号、类型-指针-类型转换告诉你:“等等,不要这样做。也许你的代码设计有问题!”。
Also, not all C++ compilers allow type aliasing (either achieved by casting or by unions).
此外,并非所有 C++ 编译器都允许类型别名(通过强制转换或联合实现)。
回答by cpp
With reinterpret_cast
you can cast a pointer type to any other pointer type, for example
you can cast float
pointer to int
pointer:
随着reinterpret_cast
您可以将指针类型为任何其他指针类型,例如,你可以投float
指针int
的指针:
float *a = new int(0);
int* b = reinterpret_cast<int*>(a);
回答by lpapp
In the second case, it is not a cast from the value a
to b
. In fact, that is just a conversion. b
will not point to x
and pretend that it points to a float. Conversion constructs a new value of type int and assigns it the value from a
.
在第二种情况下,它不是从值a
到的强制转换b
。事实上,这只是一种转换。b
不会指向x
并假装它指向一个浮点数。转换构造一个 int 类型的新值,并将值从 分配给它a
。
There are several ways to do this conversion correctly in C++.
有几种方法可以在 C++ 中正确地进行这种转换。
One is simply to use static cast as usual. This is the recommended solution:
一种是像往常一样简单地使用静态转换。这是推荐的解决方案:
int b = static_cast<int>(a);
You could use reinterpret_cast in the following way below. Note this is doing the reinterpretation for the bit patter, and not conversion unlike the alternatives mentioned:
您可以通过以下方式使用 reinterpret_cast。请注意,这是对位模式的重新解释,而不是与提到的替代方案不同的转换:
int b = reinterpret_cast<int&>(a);
You could also use the C style cast:
您还可以使用 C 样式转换:
int b = (int)a;
You could also use the C++ function style casting:
您还可以使用 C++ 函数样式转换:
int b = int(a);
You could get the implicit conversation as well, albeit it might generate a warning:
您也可以获得隐式对话,尽管它可能会产生警告:
int b = a;
The static cast is recommended in this special case, but at least do not use the implicit conversation, nor the C style in C++.
在这种特殊情况下建议使用静态转换,但至少不要使用隐式对话,也不要使用 C++ 中的 C 风格。