C++:无法 static_cast 从 double* 到 int*
当我尝试使用 static_cast 将 double* 转换为 int* 时,出现以下错误:
invalid static_cast from type ‘double*’ to type ‘int*’
这是代码:
#include <iostream>
int main()
{
double* p = new double(2);
int* r;
r=static_cast<int*>(p);
std::cout << *r << std::endl;
}
我知道在 double 和 int 之间转换会出现问题,但为什么转换时会出现问题在 double* 和 int* 之间?
When I try to use a static_cast to cast a double* to an int*, I get the following error:
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*?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该使用
reinterpret_cast
来转换指针,即当然这没有任何意义,
除非您想以
int
级别查看double
!你会得到一些奇怪的输出,我认为这不是你想要的。如果您想将p
指向的 value 转换为int
,那么,r
也不是 <已分配,以便您可以执行以下操作之一:或者
You should use
reinterpret_cast
for casting pointers, i.e.Of course this makes no sense,
unless you want take a
int
-level look at adouble
! 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 byp
to anint
then,Also,
r
is not allocated so you can do one of the following:Or
除了都是指针之外,
double*
和int*
没有任何共同点。对于指向任何不同结构的Foo*
和Bar*
指针类型,您可以说同样的事情。static_cast
表示源类型的指针可以用作目标类型的指针,这需要子类型关系。Aside from being pointers,
double*
andint*
have nothing in common. You could say the same thing forFoo*
andBar*
pointer types to any dissimilar structures.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.支持浮点到整数的转换,因此
int a = static_cast(5.2)
就可以了。然而,这是一种转换——底层数据类型完全不兼容。您要求运行时将指向 8 字节结构的指针转换为指向 4 字节结构的指针,但它无法以任何有意义的方式执行此操作。话虽如此,如果您确实想将双精度解释为整数,则 int* r = reinterpret_cast(p) 会很好地工作。
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.That having been said, if you really want to interpret your double as an integer,
int* r = reinterpret_cast<int*>(p)
will work fine.您可以使用
static_cast<>
在 double 和 int 之间进行转换,但不能在指向不同类型的指针之间进行转换。您可以使用static_cast<>
将任何指针类型与void *
相互转换。其基本原理可能是
int *
和double *
通常实际上是数组,并且实现不知道数组有多大。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 fromvoid *
withstatic_cast<>
.The rationale may be that
int *
anddouble *
are often effectively arrays, and the implementation doesn't know how big the array is.因为你使用了
double *
而不是double
后面的
*
意味着你正在声明一个指针,这与常规的 double 有很大不同。C++ 无法安全地将指针 static_cast 指向不同类型的指针。
如果你想做这种事情,你必须首先取消引用该变量。
您必须使用
new
因为双精度数和整数不能驻留在同一内存空间(合理)Because you used
double *
instead ofdouble
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.
If you are wanting to do this kinda thing, you must first dereference the variable.
You must use
new
because a double and an integer can not reside in the same memory space(sanely)