int i = 1000;
void *p = &i;
int *x = static_cast<int*>(p);
int *y = reinterpret_cast<int*>(p);
which cast should be used to convert from void*
to int*
and why?
应该使用哪个cast从void*转换为int*,为什么?
4 个解决方案
#1
13
static_cast
provided that you know (by design of your program) that the thing pointed to really is an int
.
static_cast提供了您知道(通过程序的设计)指向的对象实际上是一个int。
static_cast is designed to reverse any implicit conversion. You converted to void*
implicitly, therefore you can (and should) convert back with static_cast
if you know that you really are just reversing an earlier conversion.
static_cast被设计为反转任何隐式转换。您可以隐式地转换为void*,因此,如果您知道您实际上只是在逆转之前的转换,那么您可以(而且应该)使用static_cast进行转换。
With that assumption, nothing is being reinterpreted - void
is an incomplete type, meaning that it has no values, so at no point are you interpreting either a stored int value "as void" or a stored "void value" as int. void*
is just an ugly way of saying, "I don't know the type, but I'm going to pass the pointer on to someone else who does".
假设,没有被重新解释,空是一个不完整的类型,也就是说,它没有价值,所以在没有意义你解释存储int值“无效”或存储“空值”int. void *只是一个丑陋的说法,“我不知道类型,但我要将指针传递给别人的人”。
reinterpret_cast
if you've omitted details that mean you might actually be reading memory using a type other than the type is was written with, and be aware that your code will have limited portability.
如果您忽略了一些细节,这些细节意味着您可能正在使用另一种类型(而不是使用与该类型一起编写的类型)读取内存,那么请重新解释一下,并注意您的代码的可移植性是有限的。
By the way, there are not very many good reasons for using a void*
pointer in this way in C++. C-style callback interfaces can often be replaced with either a template function (for anything that resembles the standard function qsort
) or a virtual interface (for anything that resembles a registered listener). If your C++ code is using some C API then of course you don't have much choice.
顺便说一下,在c++中使用void*指针的理由并不多。c风格的回调接口通常可以用模板函数(任何类似于标准函数qsort的东西)或虚拟接口(任何类似于注册侦听器的东西)来替换。如果您的c++代码使用了一些C API,那么当然您没有太多的选择。
#2
1
In current C++, you can't use reinterpret_cast
like in that code. For a conversion of void*
to int*
you can only use static_cast
(or the equivalent C-style cast).
在当前c++中,不能像在代码中那样使用reinterpret_cast。对于将void*转换为int*,只能使用static_cast(或等效的c样式cast)。
For a conversion between different function type pointers or between different object type pointers you need to use reinterpret_cast
.
对于不同函数类型指针之间或不同对象类型指针之间的转换,需要使用reinterpret_cast。
In C++0x, reinterpret_cast<int*>(p)
will be equivalent to static_cast<int*>(p)
. It's probably incorporated in one of the next WPs.
在c++ 0x中,reinterpret_cast
It's a misconception that reinterpret_cast<T*>(p)
would interpret the bits of p
as if they were representing a T*
. In that case it will read the value of p
using p
's type, and that value is then converted to a T*
. An actual type-pun that directly reads the bits of p
using the representation of type T*
only happens when you cast to a reference type, as in reinterpret_cast<T*&>(p)
.
这是一种误解,认为reviewt_cast
As far as I know, all current compilers allow to reinterpret_cast
from void*
and behave equivalent to the corresponding static_cast
, even though it is not allowed in current C++03. The amount of code broken when it's rejected will be no fun, so there is no motivation for them to forbid it.
据我所知,所有当前的编译器都允许从void*重新解释t_cast,并使其行为等效于相应的static_cast,尽管在当前c++ 03中不允许这样做。当代码被拒绝时,会有大量的代码被破坏,这并不是什么有趣的事情,所以他们没有理由禁止它。
#3
0
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? gives some good details.
什么时候应该使用static_cast、dynamic_cast、const_cast和reinterpret_cast ?给一些好的细节。
#4
-1
From the semantics of your problem, I'd go with reinterpret, because that's what you actually do.
从你的问题的语义来看,我要重新解释一下,因为那是你真正要做的。
#1
13
static_cast
provided that you know (by design of your program) that the thing pointed to really is an int
.
static_cast提供了您知道(通过程序的设计)指向的对象实际上是一个int。
static_cast is designed to reverse any implicit conversion. You converted to void*
implicitly, therefore you can (and should) convert back with static_cast
if you know that you really are just reversing an earlier conversion.
static_cast被设计为反转任何隐式转换。您可以隐式地转换为void*,因此,如果您知道您实际上只是在逆转之前的转换,那么您可以(而且应该)使用static_cast进行转换。
With that assumption, nothing is being reinterpreted - void
is an incomplete type, meaning that it has no values, so at no point are you interpreting either a stored int value "as void" or a stored "void value" as int. void*
is just an ugly way of saying, "I don't know the type, but I'm going to pass the pointer on to someone else who does".
假设,没有被重新解释,空是一个不完整的类型,也就是说,它没有价值,所以在没有意义你解释存储int值“无效”或存储“空值”int. void *只是一个丑陋的说法,“我不知道类型,但我要将指针传递给别人的人”。
reinterpret_cast
if you've omitted details that mean you might actually be reading memory using a type other than the type is was written with, and be aware that your code will have limited portability.
如果您忽略了一些细节,这些细节意味着您可能正在使用另一种类型(而不是使用与该类型一起编写的类型)读取内存,那么请重新解释一下,并注意您的代码的可移植性是有限的。
By the way, there are not very many good reasons for using a void*
pointer in this way in C++. C-style callback interfaces can often be replaced with either a template function (for anything that resembles the standard function qsort
) or a virtual interface (for anything that resembles a registered listener). If your C++ code is using some C API then of course you don't have much choice.
顺便说一下,在c++中使用void*指针的理由并不多。c风格的回调接口通常可以用模板函数(任何类似于标准函数qsort的东西)或虚拟接口(任何类似于注册侦听器的东西)来替换。如果您的c++代码使用了一些C API,那么当然您没有太多的选择。
#2
1
In current C++, you can't use reinterpret_cast
like in that code. For a conversion of void*
to int*
you can only use static_cast
(or the equivalent C-style cast).
在当前c++中,不能像在代码中那样使用reinterpret_cast。对于将void*转换为int*,只能使用static_cast(或等效的c样式cast)。
For a conversion between different function type pointers or between different object type pointers you need to use reinterpret_cast
.
对于不同函数类型指针之间或不同对象类型指针之间的转换,需要使用reinterpret_cast。
In C++0x, reinterpret_cast<int*>(p)
will be equivalent to static_cast<int*>(p)
. It's probably incorporated in one of the next WPs.
在c++ 0x中,reinterpret_cast
It's a misconception that reinterpret_cast<T*>(p)
would interpret the bits of p
as if they were representing a T*
. In that case it will read the value of p
using p
's type, and that value is then converted to a T*
. An actual type-pun that directly reads the bits of p
using the representation of type T*
only happens when you cast to a reference type, as in reinterpret_cast<T*&>(p)
.
这是一种误解,认为reviewt_cast
As far as I know, all current compilers allow to reinterpret_cast
from void*
and behave equivalent to the corresponding static_cast
, even though it is not allowed in current C++03. The amount of code broken when it's rejected will be no fun, so there is no motivation for them to forbid it.
据我所知,所有当前的编译器都允许从void*重新解释t_cast,并使其行为等效于相应的static_cast,尽管在当前c++ 03中不允许这样做。当代码被拒绝时,会有大量的代码被破坏,这并不是什么有趣的事情,所以他们没有理由禁止它。
#3
0
When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? gives some good details.
什么时候应该使用static_cast、dynamic_cast、const_cast和reinterpret_cast ?给一些好的细节。
#4
-1
From the semantics of your problem, I'd go with reinterpret, because that's what you actually do.
从你的问题的语义来看,我要重新解释一下,因为那是你真正要做的。