C++:不能 static_cast 从 double* 到 int*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2473628/
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
C++: can't static_cast from double* to int*
提问by samoz
When I try to use a static_cast to cast a double* to an int*, I get the following error:
当我尝试使用 static_cast 将 double* 转换为 int* 时,出现以下错误:
invalid static_cast from type ‘double*' to type ‘int*'
Here is the code:
这是代码:
#include <iostream>
int main()
{
double* p = new double(2);
int* r;
r=static_cast<int*>(p);
std::cout << *r << std::endl;
}
I understand that there would be problems converting between a double and an int, but why is there a problem converting between a double* and an int*?
我知道在 double 和 int 之间转换会出现问题,但是为什么在 double* 和 int* 之间转换会出现问题?
回答by Jacob
You should use reinterpret_cast
for casting pointers, i.e.
您应该reinterpret_cast
用于铸造指针,即
r = reinterpret_cast<int*>(p);
Of course this makes no sense,
这当然没有意义,
unless you want take a int
-level look at a double
! You'll get some weird output and I don't think this is what you intended. If you want to cast the valuepointed to by p
to an int
then,
除非您想int
对double
! 你会得到一些奇怪的输出,我认为这不是你想要的。如果你想投的价值指向p
到int
那时,
*r = static_cast<int>(*p);
Also, r
is not allocatedso you can do one of the following:
此外,r
未分配,因此您可以执行以下操作之一:
int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;
Or
或者
int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;
回答by Potatoswatter
Aside from being pointers, double*
and int*
have nothing in common. You could say the same thing for Foo*
and Bar*
pointer types to any dissimilar structures.
除了作为指针,double*
并int*
没有什么共同点。你可能会说同样的事情Foo*
和Bar*
指针类型的任何不同的结构。
static_cast
means that a pointer of the source type can be usedas a pointer of the destination type, which requires a subtype relationship.
static_cast
意味着源类型的指针可以被用来作为目的地类型,这需要一个亚型关系的指针。
回答by Dathan
Floating point-to-integer conversion is supported, so int a = static_cast<int>(5.2)
is fine. However, it's a conversion - the underlying data types are completely incompatible. What you're asking is for the runtime to convert a pointer to an 8-byte structure to a pointer to a 4-byte structure, which it can't do in any meaningful way.
支持浮点到整数转换,所以int a = static_cast<int>(5.2)
很好。然而,这是一种转换——底层数据类型完全不兼容。您要求运行时将指向 8 字节结构的指针转换为指向 4 字节结构的指针,但它无法以任何有意义的方式执行此操作。
That having been said, if you really want to interpret your double as an integer, int* r = reinterpret_cast<int*>(p)
will work fine.
话虽如此,如果您真的想将 double 解释为整数,则int* r = reinterpret_cast<int*>(p)
可以正常工作。
回答by David Thornley
You can convert between a double and an int with static_cast<>
, but not between pointers to different types. You can convert any pointer type to or from void *
with static_cast<>
.
您可以在 double 和 int 之间进行转换static_cast<>
,但不能在指向不同类型的指针之间进行转换。您可以将任何指针类型void *
与static_cast<>
.
The rationale may be that int *
and double *
are often effectively arrays, and the implementation doesn't know how big the array is.
基本原理可能是int *
并且double *
通常是有效的数组,并且实现不知道数组有多大。
回答by Earlz
Because you used double *
instead of double
因为你用double *
而不是double
The *
after it means that you are declaring a pointer, which is vastly different from a regular double.
在*
经过这意味着你将指针声明,这是从一个普通的双大不相同。
C++ can not safely static_cast a pointer to a different type of pointer like that.
C++ 不能安全地将指针 static_cast 指向不同类型的指针。
If you are wanting to do this kinda thing, you must first dereference the variable.
如果你想做这种事情,你必须首先取消对变量的引用。
r=new int(static_cast<int>(*p));
You must use new
because a double and an integer can not reside in the same memory space(sanely)
您必须使用,new
因为双精度和整数不能驻留在同一个内存空间中(理智地)