如何在C ++中使用reinterpret_cast? [英] How to use reinterpret_cast in C++?

查看:262
本文介绍了如何在C ++中使用reinterpret_cast?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道C ++中的 reinterpret_cast 可以这样使用:

I know the reinterpret_cast in C++ can be used in this way:

float a = 0;
int b = *reinterpret_cast<int*>(&a);

但是为什么不能直接投放?

But why cannot cast it directly?

float a = 0;
int b = reinterpret_cast<int>(a);

error: invalid cast from type 'float' to type 'int'


推荐答案

你不能reinterpret_cast在你给的情况下,因为reinterpret_cast只需要一个int转换为指针,或反过来,并遵循额外的类似规则。

You can't reinterpret_cast in the case you give because reinterpret_cast takes only either a int to convert to a pointer, or the reverse, and follows additional similar rules.

这些规则的摘要如下: http: //en.cppreference.com/w/cpp/language/reinterpret_cast

There is a summary of these rules there: http://en.cppreference.com/w/cpp/language/reinterpret_cast

这篇关于如何在C ++中使用reinterpret_cast?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆