简体   繁体   English

C ++:不能从double *到int *的static_cast

[英]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);

Of course this makes no sense, 当然这没有道理,

unless you want take a int -level look at a double ! 除非你想看一个doubleint -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.

相关问题 C ++ static_cast从int *到void *到char * - 你能帮我理解这段代码吗? - C++ static_cast from int* to void* to char* - can you help me to understand this code? 在C ++中,static_cast之间有什么区别 <double> (a)和双(a)? - In C++, what are the differences between static_cast<double>(a) and double(a)? 为什么我不能使用static_cast <int&>将整数引用参数传递给C ++中的函数? - Why can't I use static_cast<int&> to pass an integer reference parameter to a function in C++? 返回static_cast <int> C ++和C - return static_cast<int> C++ and C 为什么在 C++ 中不允许从 int (*)(int) 到 void* 的 static_cast ? - Why is static_cast from int (*)(int) to void* not allowed in C++? static_cast复合体 <short> 复杂 <double> 在C ++中 - static_cast complex<short> to complex<double> in c++ static_cast的固定精度 <int> ()在C ++中 - fixing precision of static_cast<int>() in c++ 使用 static_cast 从 int 转换为 float 时输出不正确<float> C++ - Incorrect output when converting from int to float with static_cast<float> c++ 不能伪造来自 c++ 的托管回调(不能通过 static_cast 从 'void (GenFlowcacheTests::*)(int)' 转换为 'void(*)(int)') - cannot fake a managed callback from c++ (cannot cast from 'void (GenFlowcacheTests::*)(int)' to 'void(*)(int)' via static_cast) 为什么不能static_cast一个双void指针? - Why can't static_cast a double void pointer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM