static_cast和reinterpret_cast揭秘 收藏 本文讨论static_cast<> 和 reinterpret_cast<>。 reinterpret_cast可以转换任意一个32bit整数,包括所有的指针和整数。可以把任何整数转成指针,也可以把任何指针转成整数,以及把指针转化为任意类型的指针,威力最为强大!但不能将非32bit的实例转成指针。总之,只要是32bit的东东,怎么转都行! static_cast和dynamic_cast可以执行指针到指针的转换,或实例本身到实例本身的转换,但不能在实例和指针之间转换。static_cast只能提供编译时的类型安全,而dynamic_cast可以提供运行时类型安全。举个例子: class a;class b:a;class c。 上面三个类a是基类,b继承a,c和ab没有关系。 有一个函数void function(a&a); 现在有一个对象是b的实例b,一个c的实例c。 function(static_cast<a&>(b)可以通过而function(static<a&>(c))不能通过编译,因为在编译的时候编译器已经知道c和a的类型不符,因此static_cast可以保证安全。 下面我们骗一下编译器,先把c转成类型a b& ref_b = reinterpret_cast<b&>c; 然后function(static_cast<a&>(ref_b))就通过了!因为从编译器的角度来看,在编译时并不能知道ref_b实际上是c! 而function(dynamic_cast<a&>(ref_b))编译时也能过,但在运行时就失败了,因为dynamic_cast在运行时检查了ref_b的实际类型,这样怎么也骗不过去了。 在应用多态编程时,当我们无法确定传过来的对象的实际类型时使用dynamic_cast,如果能保证对象的实际类型,用static_cast就可以了。至于reinterpret_cast,我很喜欢,很象c语言那样的暴力转换:) dynamic_cast:动态类型转换 static_cast:静态类型转换 reinterpret_cast:重新解释类型转换 const_cast:常量类型转换 专业的上面很多了,我说说我自己的理解吧: synamic_cast一般用在父类和子类指针或应用的互相转化; static_cast一般是普通数据类型(如int m=static_cast<int>(3.14)); reinterpret_cast很像c的一般类型转换操作 const_cast是把cosnt或volatile属性去掉 . 介绍 大多程序员在学C++前都学过C,并且习惯于C风格(类型)转换。当写C++(程序)时,有时候我们在使用static_cast<>和reinterpret_cast<>时可能会有点模糊。在本文中,我将说明static_cast<>实际上做了什么,并且指出一些将会导致错误的情况。 泛型(Generic Types) float f = 12.3; float* pf = &f; // static cast<> // 成功编译, n = 12 int n = static_cast<int>(f); // 错误,指向的类型是无关的(译注:即指针变量pf是float类型,现在要被转换为int类型) //int* pn = static_cast<int*>(pf); //成功编译 void* pv = static_cast<void*>(pf); //成功编译, 但是 *pn2是无意义的内存(rubbish) int* pn2 = static_cast<int*>(pv); // reinterpret_cast<> //错误,编译器知道你应该调用static_cast<> //int i = reinterpret_cast<int>(f); //成功编译, 但是 *pn 实际上是无意义的内存,和 *pn2一样 int* pi = reinterpret_cast<int*>(pf);简而言之,static_cast<> 将尝试转换,举例来说,如float-到-integer,而reinterpret_cast<>简单改变编译器的意图重新考虑那个对象作为另一类型。 指针类型(Pointer Types) 指针转换有点复杂,我们将在本文的剩余部分使用下面的类: class CBaseX { public: int x; CBaseX() { x = 10; } void foo() { printf("CBaseX::foo() x=%d/n", x); } }; class CBaseY { public: int y; int* py; CBaseY() { y = 20; py = &y; } void bar() { printf("CBaseY::bar() y=%d, *py=%d/n", y, *py); } }; class CDerived : public CBaseX, public CBaseY { public: int z; };情况1:两个无关的类之间的转换 // Convert between CBaseX* and CBaseY* // CBaseX* 和 CBaseY*之间的转换 CBaseX* pX = new CBaseX(); // Error, types pointed to are unrelated // 错误, 类型指向是无关的 // CBaseY* pY1 = static_cast<CBaseY*>(pX); // Compile OK, but pY2 is not CBaseX // 成功编译, 但是 pY2 不是CBaseX CBaseY* pY2 = reinterpret_cast<CBaseY*>(pX); // System crash!! // 系统崩溃!! // pY2->bar();正如我们在泛型例子中所认识到的,如果你尝试转换一个对象到另一个无关的类static_cast<>将失败,而reinterpret_cast<>就总是成功“欺骗”编译器:那个对象就是那个无关类。 情况2:转换到相关的类 1. CDerived* pD = new CDerived(); 2. printf("CDerived* pD = %x/n", (int)pD); 3. 4. // static_cast<> CDerived* -> CBaseY* -> CDerived* //成功编译,隐式static_cast<>转换 5. CBaseY* pY1 = pD; 6. printf("CBaseY* pY1 = %x/n", (int)pY1); // 成功编译, 现在 pD1 = pD 7. CDerived* pD1 = static_cast<CDerived*>(pY1); 8. printf("CDerived* pD1 = %x/n", (int)pD1); 9. 10. // reinterpret_cast // 成功编译, 但是 pY2 不是 CBaseY* 11. CBaseY* pY2 = reinterpret_cast<CBaseY*>(pD); 12. printf("CBaseY* pY2 = %x/n", (int)pY2); 13. 14. // 无关的 static_cast<> 15. CBaseY* pY3 = new CBaseY(); 16. printf("CBaseY* pY3 = %x/n", (int)pY3); // 成功编译,尽管 pY3 只是一个 "新 CBaseY()" 17. CDerived* pD3 = static_cast<CDerived*>(pY3); 18. printf("CDerived* pD3 = %x/n", (int)pD3); ---------------------- 输出 --------------------------- CDerived* pD = 392fb8 CBaseY* pY1 = 392fbc CDerived* pD1 = 392fb8 CBaseY* pY2 = 392fb8 CBaseY* pY3 = 390ff0 CDerived* pD3 = 390fec 注意:在将CDerived*用隐式 static_cast<>转换到CBaseY*(第5行)时,结果是(指向)CDerived*(的指针向后) 偏移了4(个字节)(译注:4为int类型在内存中所占字节数)。为了知道static_cast<> 实际如何,我们不得不要来看一下CDerived的内存布局。 CDerived的内存布局(Memory Layout) 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zjl_1026_2001/archive/2008/04/03/2246510.aspx

如图所示,CDerived的内存布局包括两个对象,CBaseX 和 CBaseY,编译器也知道这一点。因此,当你将CDerived* 转换到 CBaseY*时,它给指针添加4个字节,同时当你将CBaseY*转换到CDerived*时,它给指针减去4。然而,甚至它即便不是一个CDerived你也可以这样做。 当然,这个问题只在如果你做了多继承时发生。在你将CDerived转换 到 CBaseX时static_cast<> 和 reinterpret_cast<>是没有区别的。 情况3:void*之间的向前和向后转换 因为任何指针可以被转换到void*,而void*可以被向后转换到任何指针(对于static_cast<> 和 reinterpret_cast<>转换都可以这样做),如果没有小心处理的话错误可能发生。 CDerived* pD = new CDerived(); printf("CDerived* pD = %x/n", (int)pD); CBaseY* pY = pD; // 成功编译, pY = pD + 4 printf("CBaseY* pY = %x/n", (int)pY); void* pV1 = pY; //成功编译, pV1 = pY printf("void* pV1 = %x/n", (int)pV1); // pD2 = pY, 但是我们预期 pD2 = pY - 4 CDerived* pD2 = static_cast<CDerived*>(pV1); printf("CDerived* pD2 = %x/n", (int)pD2); // 系统崩溃 // pD2->bar(); ---------------------- 输出 --------------------------- CDerived* pD = 392fb8 CBaseY* pY = 392fbc void* pV1 = 392fbc CDerived* pD2 = 392fbc 一旦我们已经转换指针为void*,我们就不能轻易将其转换回原类。在上面的例子中,从一个void* 返回CDerived*的唯一方法是将其转换为CBaseY*然后再转换为CDerived*。 但是如果我们不能确定它是CBaseY* 还是 CDerived*,这时我们不得不用dynamic_cast<> 或typeid[2]。 注释: 1. dynamic_cast<>,从另一方面来说,可以防止一个泛型CBaseY* 被转换到CDerived*。 2. dynamic_cast<>需要类成为多态,即包括“虚”函数,并因此而不能成为void*。 参考: 1. [MSDN] C++ Language Reference -- Casting 2. Nishant Sivakumar, Casting Basics - Use C++ casts in your VC++.NET programs 3. Juan Soulie, C++ Language Tutorial: Type Casting 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zjl_1026_2001/archive/2008/04/03/2246510.aspx

static_cast和reinterpret_cast相关推荐

  1. C++类型转换: static_cast const_cast reinterpret_cast dynamic_cast

    * C++提供了四种新的类型强制: static_cast const_cast reinterpret_cast dynamic_cast 1)staic_cast静态强制: 不能在无关的指针之间进 ...

  2. C++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast

    static_cast(可能不安全):一般.对象指(引)上行/下行转换 dynamic_cast(运行时的安全检查--抛出异常/NULL):对象指(引)上行/下行转换 const_cast:   主要 ...

  3. static_cast 和 reinterpret_cast的区别以及dynamic_cast

    大多程序员在学C++前都学过C,并且习惯于C风格(类型)转换.当写 C++(程序)时,有时候我们在使用static_cast<>和reinterpret_cast<>时可能会有 ...

  4. c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast

    c++ 数据类型转换: static_cast dynamic_cast reinterpret_cast const_cast [版权声明]转载请注明出处 http://www.cnblogs.co ...

  5. 【转】C++中的static_cast ,reinterpret_cast的用法和区别

    转自:https://www.cnblogs.com/heyonggang/p/3361643.html 原文地址:https://www.cnblogs.com/heyonggang/p/33616 ...

  6. C++笔记-const与mutable、static_cast与reinterpret_cast

    目录 const与mutable static_cast与reinterpret_cast const与mutable 如下代码: #include <iostream> using na ...

  7. Qt 中static_cast 和 reinterpret_cast的区别

    Qt 中static_cast 和 reinterpret_cast的区别 1. C++中的static_cast执行非多态的转换,用于代替C中通常的转换操作.因此,常做为隐式类型转换使用.比如: i ...

  8. C++总结:static_cast ,reinterpret_cast

    static_cast ,reinterpret_cast 用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type ...

  9. static_cast 与reinterpret_cast

    static_cast 用法:static_cast < type-id > ( expression ) 该运算符把expression转换为type-id类型,但没有运行时类型检查来保 ...

  10. static_cast 和 reinterpret_cast

    <<static_cast 和 reinterpret_cast>> 作者: 阙荣文(querw@sina.com) C/C++是强类型语言,不同类型之间的相互转换是比较麻烦的 ...

最新文章

  1. Java裁剪压缩PNG图片,透明背景色变黑问题解决
  2. IQueryable接口与IEnumberable接口的区别
  3. 学习Haskell的一些资料
  4. Quartz2D简单绘制之饼状图
  5. 图形大小_光伏电池正面图形设计优化
  6. 双流棠湖中学怎么样_最强攻略!春节去哪儿high?双流这些免费活动别错过~
  7. C语言函数程序实例(超全)
  8. VGG16网络结构要点
  9. pico的学习之路(五)——ssd1306上显示汉字(树莓派pico实现)
  10. 自学SpringBoot二之配置文件--yml格式配置
  11. CentOS之命令方式安装向日葵与内网穿透
  12. 大合集!近两年目标跟踪资源全汇总(论文、模型代码、优秀实验室)
  13. Windows蓝屏为什么是蓝底白字?微软程序员揭开了秘密
  14. python实用程序育儿法下载_Python机器学习经典实例
  15. 裸 VSCode 必备插件
  16. AMD将推出7纳米GPU Vega,专为深度学习和机器学习打造
  17. 洪志鹏专栏> 我爱比尔盖兹
  18. 多模态神经成像之EEG-fMRI同步
  19. 理解Java ClassLoader机制 |用Java说话,人气战胜时间!Come On
  20. ## modelsim与quartus 2 联合仿真,出现Error loading design

热门文章

  1. [AirFlow]AirFlow使用指南一 安装与启动
  2. 「雅礼集训」市场--势能线段树
  3. 关于AS报 主版本 52 比 51 新, 此编译器支持最新的主版本。 建议升级此编译器 问题
  4. 电路的基本概念及基本定律
  5. ManytoMany字段属性through和through_fields
  6. 数学之美(二)Java实现绚烂绽放
  7. Shuffle 操作
  8. Java 静态多分派动态单分派
  9. 《T-SQL性能调优秘笈——基于SQL Server 2012 窗口函数》导读
  10. thinkPHP3.2.3使用过程中遇到的问题收集