手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

怎样使用reinterpret_cast进行类型转换

时间:2024-10-22 05:16:14

1、首先定义一个包含两个short类型的结构体,然后将long类型强制转换为该结构体

怎样使用reinterpret_cast进行类型转换

2、点击运行查看结构体中的一个short值,该值存储long变量中的一部分

怎样使用reinterpret_cast进行类型转换

3、reinterpret_cast可以将依宏氽墓指针类型转换为int类型,如下图,定义了一个char *类型,然后强制转换为int (注意编译时添加#include <iomanip>头文件)

怎样使用reinterpret_cast进行类型转换

4、通过点击运行查看结果,整数类型的变量和指针变量的值是一样的

怎样使用reinterpret_cast进行类型转换

5、同样的可以将整型变量重新转换为指针变量类型

怎样使用reinterpret_cast进行类型转换

6、运行结果可以得出,整型变量转换过来的指针类型,成功输出原来定义的字符串

怎样使用reinterpret_cast进行类型转换

7、实现如下图的代码段,尝试去掉const的整型指针

怎样使用reinterpret_cast进行类型转换

8、点击编译报错,提示无法将const int *转换为int *,说明reinterpret_cast无法去掉const标示

怎样使用reinterpret_cast进行类型转换
© 手抄报圈