static_cast 和 reinterpret_cast的区别

news/2024/6/29 12:02:39 标签: casting, 编译器, types, float, reference, class
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

 static_cast 和 reinterpret_cast的区别

大多程序员在学C++前都学过C,并且习惯于C风格(类型)转换。当写C++(程序)时,有时候我们在使用static_cast<>和reinterpret_cast<>时可能会有点模糊。在本文中,我将说明static_cast<>实际上做了什么,并且指出一些将会导致错误的情况。

泛型(Generic Types)

        class="tags" href="/tags/FLOAT.html" title=float>float f = 12.3;
        class="tags" href="/tags/FLOAT.html" title=float>float* pf = &f;
        // static cast<>
        // 成功编译, n = 12
        int n = static_cast<int>(f);
        // 错误,指向的类型是无关的(译注:即指针变量pf是class="tags" href="/tags/FLOAT.html" title=float>float类型,现在要被转换为int类型)
        //int* pn = static_cast<int*>(pf);
        //成功编译
        void* pv = static_cast<void*>(pf);
        //成功编译, 但是 *pn2是无意义的内存(rubbish)
        int* pn2 = static_cast<int*>(pv);// reinterpret_cast<>
        //错误,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器知道你应该调用static_cast<>
        //int i = reinterpret_cast<int>(f);
        //成功编译, 但是 *pn 实际上是无意义的内存,和 *pn2一样
        int* pi = reinterpret_cast<int*>(pf);
简而言之,static_cast<> 将尝试转换,举例来说,如class="tags" href="/tags/FLOAT.html" title=float>float-到-integer,而reinterpret_cast<>简单改变class="tags" href="/tags/BianYiQi.html" title=编译器>编译器的意图重新考虑那个对象作为另一类型。

指针类型(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, class="tags" href="/tags/TYPES.html" title=types>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<>就总是成功“欺骗”class="tags" href="/tags/BianYiQi.html" title=编译器>编译器:那个对象就是那个无关类。

情况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)

 

如图所示,CDerived的内存布局包括两个对象,CBaseX 和 CBaseY,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器也知道这一点。因此,当你将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/jiangdf/archive/2009/05/21/4205481.aspx


http://www.niftyadmin.cn/n/944874.html

相关文章

C++ /C内存耗尽怎么办?

如果在申请动态内存时找不到足够大的内存块&#xff0c;malloc 和new 将返回NULL 指针&#xff0c;宣告内存申请失败。通常有三种方式处理“内存耗尽”问题。&#xff08;1&#xff09;判断指针是否为NULL&#xff0c;如果是则马上用return 语句终止本函数。例如&#xff1a;vo…

Ehcache 1.5.0 User Guide - Cache Configuration 缓存配置(3)

Ehcache 1.5.0 User Guide - Cache Configuration 缓存配置(3) &#xff08;Ehcache 1.5.0 用户指南&#xff09; E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 diskExpiryThreadIntervalSeconds: The number of seconds between runs o…

Ehcache 1.5.0 User Guide - Cache Eviction Algorithms (缓存移出算法)

Ehcache 1.5.0 User Guide - Cache Eviction Algorithms (缓存移出算法) Ehcache 1.5.0 用户指南&#xff09; E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 7、Cache Eviction Algorithms (缓存移出算法) 7.1 Eviction&#xff08;逐出…

Ehcache 1.5.0 User Guide - Code Samples 代码实例 (1)

Ehcache 1.5.0 User Guide - Code Samples代码实例1 Ehcache 1.5.0 用户指南&#xff09; E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 8、Code Samples&#xff08;代码实例&#xff09; This page shows some of the more common cod…

增加缓存的大小的一个例子

当你在某个缓存中存储数据时&#xff0c;常常需要在运行时调整该缓存的大小&#xff0c;以便能容纳更多的数据。 下面是一个增加初始缓存大小的例子&#xff1a; 仔细琢磨一下........ // console.cpp : Defines the entry point for the console application.//#include &quo…

Ehcache 1.5.0 User Guide - Code Samples 代码实例2

Ehcache 1.5.0 User Guide - Code Samples 代码实例2 Ehcache 1.5.0 用户指南&#xff09; E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 8.1.3 Adding and Removing Caches Programmatically&#xff08;编程的添加和移除缓存&#xf…

Ehcache 1.5.0 User Guide - Code Samples 代码实例 3

Ehcache 1.5.0 User Guide - Code Samples 代码实例 3 (Ehcache 1.5.0 用户指南) E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 8.2 Using Caches All of these examples refer to manager, which is a reference to a CacheManager,…

Ehcache 1.5.0 User Guide - Java Requirements and Dependencies JAVA要求和依赖

Ehcache 1.5.0 User Guide - Java Requirements and Dependencies JAVA要求和依赖 (Ehcache 1.5.0 用户指南) E_mail:jianglike18163.con Blog: http://blog.csdn.net/jianglike18 qq:29396597 9、Java Requirements and Dependencies &#xff08;JAVA要求和依赖&#…