[英]Casting double* to int* in C++
The title's pretty self explanatory. 标题非常自我解释。 I want to cast a type
double *
into a type int *
. 我想将类型
double *
转换为类型int *
。 I realize that I can use the C-type cast (int *)
to do what I want, but is there a way to do this cast using C++ type casting ie static_cast
etc? 我意识到我可以使用C-type强制转换
(int *)
来做我想要的,但有没有办法使用C ++类型转换,即static_cast
等进行此转换?
You can perform this cast using a reinterpret_cast
: 您可以使用
reinterpret_cast
执行此reinterpret_cast
:
int* veryUnsafePointer = reinterpret_cast<int*>(myDoublePointer);
Be aware that this does not give you back an integer representation of the double
being pointed at; 请注意,这并不会返回指向的
double
的整数表示形式; instead, the integer's value will be dependent on the binary representation of the double
and the endianness of the system. 相反,整数的值将取决于
double
的二进制表示和系统的endianness。
Hope this helps! 希望这可以帮助!
You need to use reinterpret_cast<int *>(ptr)
. 您需要使用
reinterpret_cast<int *>(ptr)
。
I hope you really know what you're doing though. 我希望你真的知道你在做什么。 There's very little reason to do such a cast, especially when it's likely a
double
and an int
are different sizes. 没有理由做这样的演员,特别是当它可能是一个
double
和一个int
是不同的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.