【转】static_cast和reinterpret_cast

  1   static_cast和reinterpret_cast揭秘 收藏
  2 本文讨论static_cast<> 和 reinterpret_cast<>。
  3
  4 reinterpret_cast可以转换任意一个32bit整数,包括所有的指针和整数。可以把任何整数转成指针,也可以把任何指针转成整数,以及把指针转化为任意类型的指针,威力最为强大!但不能将非32bit的实例转成指针。总之,只要是32bit的东东,怎么转都行!
  5 static_cast和dynamic_cast可以执行指针到指针的转换,或实例本身到实例本身的转换,但不能在实例和指针之间转换。static_cast只能提供编译时的类型安全,而dynamic_cast可以提供运行时类型安全。举个例子:
  6 class a;class b:a;class c。
  7 上面三个类a是基类,b继承a,c和ab没有关系。
  8 有一个函数void function(a&a);
  9 现在有一个对象是b的实例b,一个c的实例c。
 10 function(static_cast<a&>(b)可以通过而function(static<a&>(c))不能通过编译,因为在编译的时候编译器已经知道c和a的类型不符,因此static_cast可以保证安全。
 11 下面我们骗一下编译器,先把c转成类型a
 12 b& ref_b = reinterpret_cast<b&>c;
 13 然后function(static_cast<a&>(ref_b))就通过了!因为从编译器的角度来看,在编译时并不能知道ref_b实际上是c!
 14 而function(dynamic_cast<a&>(ref_b))编译时也能过,但在运行时就失败了,因为dynamic_cast在运行时检查了ref_b的实际类型,这样怎么也骗不过去了。
 15 在应用多态编程时,当我们无法确定传过来的对象的实际类型时使用dynamic_cast,如果能保证对象的实际类型,用static_cast就可以了。至于reinterpret_cast,我很喜欢,很象c语言那样的暴力转换:)
 16
 17 dynamic_cast:动态类型转换
 18 static_cast:静态类型转换
 19 reinterpret_cast:重新解释类型转换
 20 const_cast:常量类型转换
 21 专业的上面很多了,我说说我自己的理解吧:
 22 synamic_cast一般用在父类和子类指针或应用的互相转化;
 23 static_cast一般是普通数据类型(如int m=static_cast<int>(3.14));
 24 reinterpret_cast很像c的一般类型转换操作
 25 const_cast是把cosnt或volatile属性去掉
 26
 27 .
 28
 29 介绍
 30 大多程序员在学C++前都学过C,并且习惯于C风格(类型)转换。当写C++(程序)时,有时候我们在使用static_cast<>和reinterpret_cast<>时可能会有点模糊。在本文中,我将说明static_cast<>实际上做了什么,并且指出一些将会导致错误的情况。
 31
 32 泛型(Generic Types)
 33
 34
 35
 36          float f = 12.3;
 37         float* pf = &f;
 38               // static cast<>
 39         // 成功编译, n = 12
 40         int n = static_cast<int>(f);
 41         // 错误,指向的类型是无关的(译注:即指针变量pf是float类型,现在要被转换为int类型)        //int* pn = static_cast<int*>(pf);
 42         //成功编译
 43          void* pv = static_cast<void*>(pf);
 44         //成功编译, 但是 *pn2是无意义的内存(rubbish)
 45          int* pn2 = static_cast<int*>(pv);
 46               // reinterpret_cast<>
 47         //错误,编译器知道你应该调用static_cast<>
 48         //int i = reinterpret_cast<int>(f);
 49         //成功编译, 但是 *pn 实际上是无意义的内存,和 *pn2一样
 50          int* pi = reinterpret_cast<int*>(pf);简而言之,static_cast<> 将尝试转换,举例来说,如float-到-integer,而reinterpret_cast<>简单改变编译器的意图重新考虑那个对象作为另一类型。
 51
 52 指针类型(Pointer Types)
 53
 54 指针转换有点复杂,我们将在本文的剩余部分使用下面的类:
 55
 56 class CBaseX
 57       {
 58       public:
 59       int x;
 60       CBaseX() { x = 10; }
 61       void foo() { printf("CBaseX::foo() x=%d/n", x); }
 62       };
 63       class CBaseY
 64         {
 65         public:
 66         int y;
 67         int* py;
 68         CBaseY() { y = 20; py = &y; }
 69         void bar() { printf("CBaseY::bar() y=%d, *py=%d/n", y, *py);
 70         }
 71         };
 72       class CDerived : public CBaseX, public CBaseY
 73         {
 74         public:
 75         int z;
 76         };情况1:两个无关的类之间的转换
 77
 78
 79       // Convert between CBaseX* and CBaseY*
 80       // CBaseX* 和 CBaseY*之间的转换
 81       CBaseX* pX = new CBaseX();
 82       // Error, types pointed to are unrelated
 83       // 错误, 类型指向是无关的
 84       // CBaseY* pY1 = static_cast<CBaseY*>(pX);
 85       // Compile OK, but pY2 is not CBaseX
 86       // 成功编译, 但是 pY2 不是CBaseX
 87       CBaseY* pY2 = reinterpret_cast<CBaseY*>(pX);
 88       // System crash!!
 89       // 系统崩溃!!
 90       // pY2->bar();正如我们在泛型例子中所认识到的,如果你尝试转换一个对象到另一个无关的类static_cast<>将失败,而reinterpret_cast<>就总是成功“欺骗”编译器:那个对象就是那个无关类。
 91
 92 情况2:转换到相关的类
 93
 94       1. CDerived* pD = new CDerived();
 95       2. printf("CDerived* pD = %x/n", (int)pD);
 96       3.
 97       4. // static_cast<> CDerived* -> CBaseY* -> CDerived*
 98       //成功编译,隐式static_cast<>转换
 99       5. CBaseY* pY1 = pD;
100       6. printf("CBaseY* pY1 = %x/n", (int)pY1);
101       // 成功编译, 现在 pD1 = pD
102       7. CDerived* pD1 = static_cast<CDerived*>(pY1);
103       8. printf("CDerived* pD1 = %x/n", (int)pD1);
104       9.
105       10. // reinterpret_cast
106       // 成功编译, 但是 pY2 不是 CBaseY*
107       11. CBaseY* pY2 = reinterpret_cast<CBaseY*>(pD);
108       12. printf("CBaseY* pY2 = %x/n", (int)pY2);
109       13.
110       14. // 无关的 static_cast<>
111       15. CBaseY* pY3 = new CBaseY();
112       16. printf("CBaseY* pY3 = %x/n", (int)pY3);
113       // 成功编译,尽管 pY3 只是一个 "新 CBaseY()"
114       17. CDerived* pD3 = static_cast<CDerived*>(pY3);
115       18. printf("CDerived* pD3 = %x/n", (int)pD3);      ---------------------- 输出 ---------------------------
116       CDerived* pD = 392fb8
117       CBaseY* pY1 = 392fbc
118       CDerived* pD1 = 392fb8
119       CBaseY* pY2 = 392fb8
120       CBaseY* pY3 = 390ff0
121       CDerived* pD3 = 390fec
122       注意:在将CDerived*用隐式 static_cast<>转换到CBaseY*(第5行)时,结果是(指向)CDerived*(的指针向后) 偏移了4(个字节)(译注:4为int类型在内存中所占字节数)。为了知道static_cast<> 实际如何,我们不得不要来看一下CDerived的内存布局。
123
124 CDerived的内存布局(Memory Layout)
125
126
127 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zjl_1026_2001/archive/2008/04/03/2246510.aspx

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

【转】static_cast和reinterpret_cast的相关文章

c++强制类型转换:dynamic_cast、const_cast 、static_cast、reinterpret_cast

c++强制类型转换:dynamic_cast.const_cast .static_cast.reinterpret_cast 博客分类: C/C++ CC++C#编程数据结构 dynamic_cast:   通常在基类和派生类之间转换时使用const_cast:   主要针对const和volatile的转换static_cast:   一般的转换(no run-time check)通常,如果你不知道该用哪个,就用这个.   reinterpret_cast:   用于进行没有任何关联之间的

static_cast和reinterpret_cast

static_cast和reinterpret_cast 相同点:都是暴力转换,从一个类型转换为另一个类型,对于类指针不会保证安全性 static_cast和reinterpret_cast的区别主要在于多重继承,比如 1 2 3 4 5 6 7 8 9 10 11 class A {     public:     int m_a; }; class B {     public:     int m_b; }; class C : public A, public B {}; 那么对于以下代

const_cast,static_cast,dynamic_cast,reinterpret_cast的区别

C++继承了C中的隐式和显式转换的方式.但这种转换并不是安全和严格的, 加上C++本身对象模型的复杂性,C++增加了四个显示转换的关键字.(C++是强类型语言) 经过编码测试,小结如下: const_cast:仅用于去掉完全同类型的const,volatile约束,不能含有任何其它类型的转换,若不含约束也可以相当于没转换static_cast:使用类型可在继承的方向上向上或向下转换,不进行安全检查. 子类转父类,值.引用.指针形式均可,其中指针还可以是常量 父类转子类,值不可以,引用和指针均可,

static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较

隐式转换(implicit conversion) short a=2000; int b; b=a; short是两字节,int是四字节,由short型转成int型是宽化转换(bit位数增多),编译器没有warning,如下图所示.宽化转换(如char到int,int到long long,int到float,float到double,int到double等)构成隐式转换,编译器允许直接转换. 但若反过来 double a=2000; short b; b=a; 此时,是从8字节的double型

Qt 中C++ static_cast 和 reinterpret_cast的区别

1.C++中的static_cast执行非多态的转换,用于代替C中通常的转换操作.因此,被做为隐式类型转换使用.比如: int i; float f = 166.7f; i = static_cast<int>(f); 此时结果,i的值为166. 2.C++中的reinterpret_cast主要是将数据从一种类型的转换为另一种类型.所谓“通常为操作数的位模式提供较低层的重新解释”也就是说将数据以二进制存在形式的重新解释.比如: int i; char *p = "This is a

static_cast, dynamic_cast和reinterpret_cast的区别和应用 [转]

其实不仅仅是一个转载,还是一个融合 今天的一个考题为:#include <iostream>using namespace std; class BClass{public:    BClass(){};    virtual ~BClass(){};    virtual void OutPut(int i){cout<<"BClass::ouput is "<<i<<endl;} };class son1:public BClass{

总结C++中的所有强制转换函数(const_cast,reinterpret_cast,static_cast,dynamic_cast)

做个笔记:源自论坛:http://bbs.csdn.net/topics/210039564 总结C++中的所有强制转换函数(const_cast,reinterpret_cast,static_cast,dynamic_cast) C 风格(C-style)强制转型如下: (T) expression // cast expression to be of type T 函数风格(Function-style)强制转型使用这样的语法: T(expression) // cast express

C++类型转化:static_cast,reinterpret_cast,dynamic_cast,const_cast

类型转换名称和语法 C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. static_cast 静态类型转换.如int转换成char reinterpreter_cast 重新解释类型 dynamic_cast 命名上理解是动态类型转换.如子类和父类之间的多态类型转换. const_cast, 字面上理解就是去const属性. 4种类型转换的格式: TYPE B = s

强制类型转换:static_cast、dynamic_cast、const_cast、reinterpret_cast

已剪辑自: https://www.cnblogs.com/chenyangchun/p/6795923.html ? ? 1. c强制转换与c++强制转换 ?c语言强制类型转换主要用于基础的数据类型间的转换,语法为: (type-id)expression//转换格式1 type-id(expression)//转换格式2 ? c++除了能使用c语言的强制类型转换外,还新增了四种强制类型转换:static_cast.dynamic_cast.const_cast.reinterpret_cas