- 浏览: 28076 次
文章分类
- 全部博客 (23)
- java (23)
- 一个不会重复的HelloWord 随机大小写.... (1)
- T-SQL查询进阶-10分钟理解游标 (1)
- java.lang.IncompatibleClassChangeError (1)
- Timer定时任务 (1)
- struts2文件下载示例 (1)
- 做了一个脚本,方便大家用sunpot做中文全文索引 (1)
- html 初始化加载flex (1)
- 一个儿童学习英语的有趣网址e-learningforkids (1)
- 如何在Apache FtpServer基础上增加或者修改FTP命令 (1)
- 空间信息到GML的转换 (1)
- 正则表达式复习 (1)
- 几个事情随便说一下 (1)
- 捕获IP数据包的Java类Jpcap (1)
- 下决心学习IBM的DB2相关 (1)
- iOS开发之缓存(一):内存缓存 (1)
- 从权限管理看互联网产品的盈利方式 (1)
- static_cast、dynamic_cast reinterpret_cast和const_ cast (1)
- ext 加载慢的原因 (1)
- 做个男人,做个成熟的男人,做个有城府的男人 (1)
- Android NDK开发入门实例 (1)
- 彩信制作之SMIL规范 (1)
- java的socket编程总结大全 (1)
- timer timertask (1)
最新评论
-
wwwang89:
楼主,写的太好了,,求demo一份:wwwang89@163. ...
iOS开发之缓存(一):内存缓存 -
qkjava:
标记一下子
做个男人,做个成熟的男人,做个有城府的男人 -
LiHu0512:
嗯,学习学习,当成熟的男人!
做个男人,做个成熟的男人,做个有城府的男人 -
mywjch:
恩,仔细看看自己有什么不足
做个男人,做个成熟的男人,做个有城府的男人 -
奥义之舞:
怎么出来这么多HTML标签
做个男人,做个成熟的男人,做个有城府的男人
static_cast、dynamic_cast reinterpret_cast和const_ cast
<span style="color: rgb(50,62,50); font-family: simsun; font-size: 14px; line-height: 21px; background-color: rgb(224,145,47);"><span style="line-height: 18px; font-size: 12px;"><span style="line-height: 18px; font-family: 'Times new roman';"><span style="line-height: 24px; font-size: 16px;"><span style="line-height: 24px; font-family: 'Times new roman';">关于强制类型转换的问题,很多书都讨论过,写的最详细的是C++
之父的《C++ 的设计和演化》。最好的解决方法就是不要使用C风格的强制类型转换,而是使用标准C++的类型转换符:static_cast, dynamic_cast。标准C++中有四个类型转换符:static_cast、dynamic_cast、reinterpret_cast、和const_cast。下面对它们一一进行介绍。</span></span></span></span></span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">static_cast</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">用法:static_cast < type-id > ( expression_r_r )</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">该运算符把expression_r_r转换为type-id类型,但没有运行时类型检查来保证转换的安全性。它主要有如下几种用法:<br>
用于类层次结构中基类和子类之间指针或引用的转换。进行上行转换(把子类的指针或引用转换成基类表示)是安全的;进行下行转换(把基类指针或引用转换成子类表示)时,由于没有动态类型检查,所以是不安全的。<br>
用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。<br>
把空指针转换成目标类型的空指针。<br>
把任何类型的表达式转换成void类型。<br>
注意:static_cast不能转换掉expression_r_r的const、volitale、或者__unaligned属性。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">dynamic_cast</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">用法:dynamic_cast < type-id > ( expression_r_r )</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">该运算符把expression_r_r转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void *;如果type-id是类指针类型,那么expression_r_r也必须是一个指针,如果type-id是一个引用,那么expression_r_r也必须是一个引用。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。<br>
class B{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">public:</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">int m_iNum;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">virtual void foo();</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">};</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">class D:public B{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">public:</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">char *m_szName[100];</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">};</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';"><wbr></wbr></span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">void func(B *pb){</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">D *pd1 = static_cast<D *>(pb);</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">D *pd2 = dynamic_cast<D *>(pb);</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">}</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">在上面的代码段中,如果pb指向一个D类型的对象,pd1和pd2是一样的,并且对这两个指针执行D类型的任何操作都是安全的;但是,如果pb指向的是一个 B类型的对象,那么pd1将是一个指向该对象的指针,对它进行D类型的操作将是不安全的(如访问m_szName),而pd2将是一个空指针。另外要注意:B要有虚函数,否则会编译出错;static_cast则没有这个限制。这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表(关于虚函数表的概念,详细可见<Inside
c++ object model>)中,只有定义了虚函数的类才有虚函数表,没有定义虚函数的类是没有虚函数表的。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">另外,dynamic_cast还支持交叉转换(cross cast)。如下代码所示。<br>
class A{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">public:</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">int m_iNum;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">virtual void f(){}</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">};</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';"><wbr></wbr></span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">class B:public A{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">};</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';"><wbr></wbr></span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">class D:public A{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">};</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';"><wbr></wbr></span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">void foo(){</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">B *pb = new B;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">pb->m_iNum = 100;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">D *pd1 = static_cast<D *>(pb); //copile error</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">D *pd2 = dynamic_cast<D *>(pb); //pd2 is NULL</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">delete pb;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">}</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">在函数foo中,使用static_cast进行转换是不被允许的,将在编译时出错;而使用 dynamic_cast的转换则是允许的,结果是空指针。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">reinpreter_cast</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">用法:reinpreter_cast<type-id> (expression_r_r)</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">该运算符的用法比较多。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">const_cast</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">用法:const_cast<type_id> (expression_r_r)</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression_r_r的类型是一样的。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">常量指针被转化成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常量对象。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">Voiatile和const类试。举如下一例:<br>
class B{</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">public:</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">int m_iNum;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">}</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">void foo(){</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">const B b1;</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">b1.m_iNum = 100; //comile error</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">B b2 = const_cast<B>(b1);</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">b2. m_iNum = 200; //fine<br>
}</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">上面的代码编译时会报错,因为b1是一个常量对象,不能对它进行改变;使用const_cast把它转换成一个常量对象,就可以对它的数据成员任意改变。注意:b1和b2是两个不同的对象。</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';">最容易理解的解释:</span>
<span style="line-height: 24px; font-size: 16px; font-family: 'Times new roman';"><wbr><wbr>dynamic_cast: <wbr>通常在基类和派生类之间转换时使用;<br><wbr><wbr>const_cast: <wbr>主要针对const和volatile的转换.<wbr><wbr><wbr><br><wbr><wbr>static_cast: <wbr>一般的转换,如果你不知道该用哪个,就用这个。<wbr><wbr><wbr><br><wbr><wbr><wbr>reinterpret_cast: <wbr>用于进行没有任何关联之间的转换,比如一个字符指针转换为一个整形数。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span>
发表评论
-
timer timertask
2012-02-08 16:07 947<p style="text-al ... -
java的socket编程总结大全
2012-02-08 15:12 734<span style="color: ... -
彩信制作之SMIL规范
2012-02-03 09:44 1056参考地址:http://blog.163.com/ch ... -
Android NDK开发入门实例
2012-01-31 13:58 1402<h1 align="center&q ... -
做个男人,做个成熟的男人,做个有城府的男人
2012-01-31 13:19 1782<p align="cente ... -
ext 加载慢的原因
2011-12-28 11:54 1360内网系统使用EXT是不应该慢,慢只能说明是EXT没有用 ... -
从权限管理看互联网产品的盈利方式
2011-12-20 10:54 655咋们做产品,基 ... -
iOS开发之缓存(一):内存缓存
2011-12-19 15:03 1049前面一片文章介绍了如何上传和下载文件,这篇文章将介绍一 ... -
下决心学习IBM的DB2相关
2011-12-19 10:04 772开始学习一本书《DB2 UDB V8.1 Certif ... -
捕获IP数据包的Java类Jpcap
2011-12-14 19:09 782... -
几个事情随便说一下
2011-12-14 11:49 7501最近上网奇慢无比,基本无法完成留言操作,所以如果有问 ... -
正则表达式复习
2011-12-13 11:14 831<p class="MsoNorma ... -
空间信息到GML的转换
2011-12-12 15:04 719<p class="MsoNorm ... -
如何在Apache FtpServer基础上增加或者修改FTP命令
2011-12-12 14:15 1446<p class="MsoN ... -
一个儿童学习英语的有趣网址e-learningforkids
2011-12-09 12:34 3297最经在一直在研究儿童如何学英语方面的东西,发现一些有意 ... -
html 初始化加载flex
2011-12-09 10:39 1344<span style="color: ... -
做了一个脚本,方便大家用sunpot做中文全文索引
2011-12-08 17:44 799sunspot ( https://github.co ... -
struts2文件下载示例
2011-12-06 10:35 860? package com.topdt.polices ... -
Timer定时任务
2011-12-06 09:29 762利用Timer类创建出一个对象,其中有schedule ... -
java.lang.IncompatibleClassChangeError
2011-12-05 18:41 3381?? 今天在修改代码之后用maven重新打包替换原来的 ...
相关推荐
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
例说 const_cast,reinterpret_cast,static_cast,dynamic_cast代码.rar
主要介绍了C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast总结,需要的朋友可以参考下
1.static_cast对类的指针只能转换有继承关系的类。对普通的指针来说只能在void*和其他指针之间转换。它还可转换简单的类型,比如int到char等。不能提供数字到指针的转换。不能提供不同类型指针之间的转换比如int*到...
C-style cast举例: int i; double d; i = (int) d;上面的代码就是本来为double类型的d,通过(int)d将其转换成整形值,并将该值赋给整形变量i (注意d本身的值并没有发生改变)。这就是典型的c-style类型转换。下面...
使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。 const_cast,字面上理解就是去const属性。 static_cast,命名上理解是静态类型转换。如int转换成char。 dynamic_cast,...
特别是在指针转换和数值转换时用到的非常多。只要编写程序的人知道自己要做什么转换,并知道应该怎样转换的话,我认为上边的转换方式非常之好。但是没有清楚的了解每个转换的细节的话,就有可能
本文详细介绍了C++中的四个用与强制类型转换的运算符:用来修改类型的const 或volatile 属性的const_cast,用来修改操作数类型的reinterpret_cast,static_cast,dynamic_cast
C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast
xxx_cast是一个统称,它指的是static_cast(静态转换),const_cast(常量转换),reinterpert_cast(重解释转换),dynamic_cast(动态转换)。本次我们就了解一下他的用途及其转换方式。 1.static_cast(静态转换) ...
什么是static_cast,dynamic_cast以及reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通过改变一个变量的类型为别的类型从而改变该变量的表示方式。为了类型转换一个简单对象为另一个对象你会使用传统...
C++四中常用强制类型转化, :static_cast、dynamic_cast、reinterpret_cast、和const_cast
.背景知识/名词解释: C++: ? static ? const ? volatile ? mutable ? namespace ? template ? explicit ? const_cast ? static_cast ? dynamic_cast ? reinterpret_cast
C++其实也具有自己的一套强制类型转换它们分明是:static_cast reinterpret_cast const_cast dynamic_cast四种类型. 那么肯定会有人好奇C++是不是闲,C语言的强制类型用的舒舒服服的,为什么要新推出来这几个? 新...
C++ 中的强制类型转换,显示转换也成为强制类型转换(cast),包括以下列名字命名的强制类型转换操作符:static_cast、dynamic_cast、const_cast、reinterpret_cast。
C++更加安全,增加了const常量、引用、四类cast转换(static_cast、dynamic_cast、const_cast、reinterpret_cast)、智能指针、try—catch等等; C++可复用性高,C++引入了模板的概念,后面在此基础上,实现了方便...