static_cast和reinterpret_cast揭秘

本文深入探讨C++中的四种类型转换操作符:static_cast、reinterpret_cast、dynamic_cast及const_cast的功能与区别,通过示例代码展示它们在不同场景下的应用,特别是针对指针和类之间的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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)




本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/zjl_1026_2001/archive/2008/04/03/2246510.aspx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值