C++:无法 static_cast 从 double* 到 int*

发布于 2024-08-25 22:02:27 字数 426 浏览 7 评论 0原文

当我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

魂牵梦绕锁你心扉 2024-09-01 22:02:27

您应该使用reinterpret_cast来转换指针,即

r = reinterpret_cast<int*>(p);

当然这没有任何意义,

除非您想以int级别查看double!你会得到一些奇怪的输出,我认为这不是你想要的。如果您想将 p 指向的 value 转换为 int,那么,

*r = static_cast<int>(*p);

r 也不是 <已分配,以便您可以执行以下操作之一:

int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;

或者

int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;

You should use reinterpret_cast for casting pointers, i.e.

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 value pointed to by p to an int then,

*r = static_cast<int>(*p);

Also, r is not allocated so you can do one of the following:

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;
情泪▽动烟 2024-09-01 22:02:27

除了都是指针之外,double*int* 没有任何共同点。对于指向任何不同结构的 Foo*Bar* 指针类型,您可以说同样的事情。

static_cast 表示源类型的指针可以用作目标类型的指针,这需要子类型关系。

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.

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.

無處可尋 2024-09-01 22:02:27

支持浮点到整数的转换,因此 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.

七七 2024-09-01 22:02:27

您可以使用 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 from void * with 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.

梦里泪两行 2024-09-01 22:02:27

因为你使用了 double * 而不是 double

后面的 * 意味着你正在声明一个指针,这与常规的 double 有很大不同。

C++ 无法安全地将指针 static_cast 指向不同类型的指针。

如果你想做这种事情,你必须首先取消引用该变量。

r=new int(static_cast<int>(*p));

您必须使用 new 因为双精度数和整数不能驻留在同一内存空间(合理)

Because you used double * instead of 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.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文