当前位置: 首页 > news >正文

reinterpret_cast and static cast

什么时候使用 reinterpret_cast?

指针类型之间的转换: 当需要将一种类型的指针转换为另一种类型的指针时,可以使用 reinterpret_cast。例如:

int* intPtr = new int(10);
void* voidPtr = reinterpret_cast<void*>(intPtr); // int* 转换为 void*
int* backToIntPtr = reinterpret_cast<int*>(voidPtr); // void* 转换回 int*

不同类型指针之间的转换: 当需要将一种类型的指针转换为完全不相关的另一种类型的指针时,例如:

int* intPtr = new int(42);
char* charPtr = reinterpret_cast<char*>(intPtr); // int* 转换为 char*

整数与指针之间的转换: 在某些底层编程场景中(如嵌入式系统或驱动开发),可能需要将整数转换为指针,或将指针转换为整数:

uintptr_t intValue = reinterpret_cast<uintptr_t>(intPtr); // 指针转换为整数
int* ptr = reinterpret_cast<int*>(intValue); // 整数转换回指针

处理类层次结构中的非多态类型: 如果类之间没有继承关系,或者不涉及虚函数(即非多态类型),可以用 reinterpret_cast 进行类型转换。例如:

struct A { int x; };
struct B { int y; };
A* a = new A{42};
B* b = reinterpret_cast<B*>(a); // A* 转换为 B*
  • 危险性:reinterpret_cast 不检查类型安全性,可能会导致未定义行为(undefined behavior)。使用时必须确保目标类型和源类型在内存布局上兼容。
  • 平台依赖性:指针大小和内存对齐规则因平台而异,因此使用 reinterpret_cast 的代码可能不可移植。
  • 仅在必要时使用:应优先考虑更安全的类型转换方式,如 static_cast 或 dynamic_cast。只有在明确需要低级别控制且了解风险时才使用 reinterpret_cast。
  • 避免在多态类中使用:如果涉及多态类(有虚函数),应使用 dynamic_cast 或 static_cast,因为 reinterpret_cast 不处理虚表或继承关系。

使用 static_cast 进行 int* 到 void* 的转换

在C++中,static_cast 允许在某些类型的指针之间进行安全的隐式转换,包括将任何类型的指针(如 int*)转换为 void*。这是因为 void* 是一种通用指针类型,标准允许从任何对象指针类型隐式转换为 void*,而 static_cast 可以显式地执行这种转换。

int* intPtr = new int(42);
void* voidPtr = static_cast<void*>(intPtr); // 合法
int* backToIntPtr = static_cast<int*>(voidPtr); // 合法,假设 voidPtr 指向 intdouble* doublePtr = new double(3.14);
void* voidPtr = static_cast<void*>(doublePtr);
int* intPtr = static_cast<int*>(voidPtr); // 编译通过,但未定义行为int* intPtr = new int(42);
char* charPtr = static_cast<char*>(intPtr); // 错误:编译失败
char* charPtr2 = reinterpret_cast<char*>(intPtr); // 合法,但需谨慎

http://www.lryc.cn/news/615923.html

相关文章:

  • Docker实战:为项目打造即开即用的宝塔LNMP环境
  • redis集群-docker环境
  • 【从源码角度深度理解 CPython 的垃圾回收机制】:第2课循环引用:标记清除-分代回收
  • 机器学习线性归回实战(单因子和多音字分别建立预测房价模型)
  • 一个基于 Next.js 和 Puppeteer 的 Markdown 转图片服务,支持 Docker 部署和 API 集成
  • Node.js面试题及详细答案120题(01-15) -- 基础概念篇
  • python | numpy小记(十):理解 NumPy 中的 `np.random.multinomial`(进阶)
  • Stlink识别不到-安装驱动
  • 医防融合中心-智慧化慢病全程管理医疗AI系统开发(下)
  • 整数规划-分支定界
  • Docker Compose 部署高可用 MongoDB 副本集集群(含 Keepalived + HAProxy 负载均衡)
  • AI编程插件对比分析:CodeRider、GitHub Copilot及其他
  • 给AI装上“翻译聚光灯”:注意力机制的机器翻译革命
  • 【精彩回顾·成都】成都 User Group×柴火创客空间:开源硬件驱动 AI 与云的创新实践!
  • 打卡day34
  • openpnp - 顶部相机如果超过6.5米影响通讯质量,可以加USB3.0信号放大器延长线
  • Spark执行计划与UI分析
  • AutoCAD 2026 的主要功能
  • 变量详解:创建初始化与内存管理
  • lesson34:深入理解Python线程:从基础到实战优化
  • XGBoost算法在机器学习中的实现
  • Android Camera 打开和拍照APK源码
  • Android 开发问题:Invalid id; ID definitions must be of the form @+id/ name
  • Android 16 KB页面大小适配的权威技术方案总结
  • Ubuntu 安装 Kibana
  • 神经机器翻译(NMT)框架:编码器-解码器(Encoder-Decoder)结构详解
  • 支持selenium的chrome driver更新到139.0.7258.66
  • 去除Edge微软浏览器与Chrome谷歌浏览器顶部出现“此版本的Windows不再支持升级Windows 10”的烦人提示
  • Elasticsearch QueryDSL 教程
  • Linux操作系统从入门到实战(十八)在Linux里面怎么查看进程