[英]C++: can't static_cast from double* to int*
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 *之间转换有问题?
You should use reinterpret_cast
for casting pointers, ie 您应该使用
reinterpret_cast
来转换指针,即
r = reinterpret_cast<int*>(p);
unless you want take a int
-level look at a double
! 除非你想看一个
double
的int
-level! You'll get some weird output and I don't think this is what you intended. 你会得到一些奇怪的输出,我不认为这是你的意图。 If you want to cast the value pointed to by
p
to an int
then, 如果要将
p
指向的值转换为int
则
*r = static_cast<int>(*p);
Also, r
is not allocated so 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;
Aside from being pointers, double*
and int*
have nothing in common. 除了作为指针之外,
double*
和int*
没有任何共同之处。 You could say the same thing for Foo*
and Bar*
pointer types to any dissimilar structures. 对于
Foo*
和Bar*
指针类型,你可以对任何不同的结构说同样的话。
static_cast
means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship. static_cast
意味着源类型的指针可以用作目标类型的指针,这需要子类型关系。
Floating point-to-integer conversion is supported, so int a = static_cast<int>(5.2)
is fine. 支持浮点到整数转换,因此
int a = static_cast<int>(5.2)
就可以了。 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.
您要问的是运行时将指向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)
将正常工作。
You can convert between a double and an int with static_cast<>
, but not between pointers to different types. 您可以使用
static_cast<>
在double和int之间进行转换,但不能在指向不同类型的指针之间进行转换。 You can convert any pointer type to or from void *
with static_cast<>
. 您可以使用
static_cast<>
将任何指针类型转换为void *
。
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 *
通常是有效的数组,并且实现不知道数组有多大。
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
因为double和integer不能驻留在同一个内存空间(sanely)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.