C\C++类型转换操作符

传统C语言的<wbr style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">内部类型转换</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"> C语言传统的转化很简单。<br style="line-height:25px"> 比如:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">doubled=5.0;<br style="line-height:25px"> inta=(int)d;</span><br style="line-height:25px"> 这个感觉和java差不多。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">C++有四个内部类型转换操作符:</wbr></span><wbr style="line-height:25px"><wbr style="line-height:25px"><span style="color:#99cc00; line-height:25px">const_cast</span><wbr style="line-height:25px"><wbr style="line-height:25px">,<span style="color:#808000; line-height:25px">reinterpret_cast</span>,<span style="color:#339966; line-height:25px">static_cast</span>,<span style="color:#008000; line-height:25px">dynamic_cast</span>。<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">const_cast</wbr></span></span><wbr style="line-height:25px"><br style="line-height:25px"> 用法:<span style="color:#0000ff; line-height:25px">const_cast&lt;type_id&gt;(expression)</span><br style="line-height:25px"> 该运算符用来修改类型的const或volatile属性。除了const或volatile修饰之外,type_id和expression的类型是一样的。<br style="line-height:25px"><span style="color:#000080; line-height:25px">一、</span><span style="color:#ff6600; line-height:25px">常量指针</span><span style="color:#000080; line-height:25px">被转化成</span><span style="color:#ff9900; line-height:25px">非常量指针</span><span style="color:#000080; line-height:25px">,并且仍然指向原来的对象;<br style="line-height:25px"> 二、</span><span style="color:#ff6600; line-height:25px">常量引用</span><span style="color:#000080; line-height:25px">被转换成</span><span style="color:#ff9900; line-height:25px">非常量引用</span><span style="color:#000080; line-height:25px">,并且仍然指向原来的对象;<br style="line-height:25px"> 三、</span><span style="color:#ff6600; line-height:25px">常量对象</span><span style="color:#000080; line-height:25px">被转换成</span><span style="color:#ff9900; line-height:25px">非常量对象</span><span style="color:#000080; line-height:25px">。</span><br style="line-height:25px"> Voiatile和const类试。举如下一例:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="line-height:25px"><span style="color:#3366FF"></span></span><span style="color:#ff6600; line-height:25px">B</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff9900; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intm_iNum;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">B</span><span style="color:#3366ff; line-height:25px">()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="line-height:25px"><span style="color:#3366FF"></span></span><span style="color:#ff6600; line-height:25px">foo</span><span style="color:#3366ff; line-height:25px">()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">constBb1;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">//b1.m_iNum=100;//comileerror</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">B&amp;b2=const_cast&lt;B&amp;&gt;(b1);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">b2.m_iNum=200;//fine?</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">int</span><span style="line-height:25px"><span style="color:#3366FF"></span></span><span style="color:#ff6600; line-height:25px">main</span><span style="color:#3366ff; line-height:25px">()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">foo();</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">return0;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 上面的代码编译时会报错,因为b1是一个常量对象,不能对它进行改变;<br style="line-height:25px"> 使用<span style="color:#ff6600; line-height:25px">const_cast</span>把它转换成一个非常量对象引用,就可以对它的数据成员任意改变。 <div style="line-height:25px"> <span style="color:#ff00ff; line-height:25px">const_cast</span>用于<span style="color:#000080; line-height:25px">类型转换掉表达式的</span><span style="color:#ff9900; line-height:25px">const</span><span style="color:#000080; line-height:25px">或</span><span style="color:#ff9900; line-height:25px">volatileness</span><span style="color:#000080; line-height:25px">属性</span>。通过使用<span style="color:#ff9900; line-height:25px">const_cast</span>,你向人们和编译器强调你通过类型转换想做的只是改变一些东西的<span style="color:#ff9900; line-height:25px">constness</span>或者<span style="color:#ff9900; line-height:25px">volatileness</span>属性。这个含义被编译器所约束。<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">如果你试图使用const_cast来完成修改constness或者volatileness属性之外的事情,你的类型转换将被拒绝<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"><span style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><wbr style="line-height:25px">reinterpret_cast</wbr></span></span><wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#ff00ff; line-height:25px">reinterpret_cast</span>是C++里的强制类型转换符。<span style="color:#003366; line-height:25px">它的本质是重新解释引用或者指针所对应的比特模型(类型),引用或者指针所指向的内存区域的数据不会变化。</span><br style="line-height:25px"> 例1:</wbr></wbr> </div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px">int*n=newint;<br style="line-height:25px"> double*d=reinterpret_cast&lt;double*&gt;(n);</span><br style="line-height:25px"> 例2:</div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px">intn=9;<br style="line-height:25px"> double&amp;d=reinterpret_cast&lt;double&amp;&gt;(n);</span><br style="line-height:25px"><span style="color:#000080; line-height:25px">在进行计算以后,d包含无用值.这是因为引用或者指针所指向的内存区域的数据不会变化。</span><span style="color:#ff6600; line-height:25px">reinterpret_cast</span><span style="color:#000080; line-height:25px">仅仅重新解释引用或者指针所对应的比特模型(类型),而</span><span style="color:#ff9900; line-height:25px">static_cast</span><span style="color:#000080; line-height:25px">却进行了二进制转换</span><br style="line-height:25px"> 例如:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">intn=9;doubled=static_cast&lt;double&gt;(n);</span> </div> <div style="line-height:25px">上面的例子中,我们将一个变量从int转换到double。这些类型的二进制表达式是不同的。<br style="line-height:25px"> 要将整数9转换到双精度整数9,<span style="color:#ff9900; line-height:25px">static_cast</span>需要正确地为双精度整数d补足比特位。其结果为9.0。<br style="line-height:25px"> 而reinterpret_cast的行为却不同:<br style="line-height:25px"> 例3:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">intn=9;<br style="line-height:25px"> doubled=reinterpret_cast&lt;double&amp;&gt;(n);</span><br style="line-height:25px"> 这次,结果有所不同.在进行计算以后,d包含无用值.这是因为reinterpret_cast仅仅重新解释引用或者指针所对应的比特模型(类型)<br style="line-height:25px"><span style="color:#000080; line-height:25px">因此,你需要谨慎使用reinterpret_cast.</span><br style="line-height:25px"> 注意:在例3中n和d的确对应不同的内存单元。<br style="line-height:25px"> 它似乎和reinterpret_cast仅仅重新解释引用或者指针所对应的比特模型(类型)相矛盾。<br style="line-height:25px"> 其实不矛盾。例3的本质如下:<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">intn=9;<br style="line-height:25px"> reinterpret_cast&lt;double&amp;&gt;temp=reinterpret_cast&lt;double&amp;&gt;(n);<br style="line-height:25px"> doubled=temp;</span><span style="color:#808080; line-height:25px">//注意这里本质是拷贝。当然n和d的确对应不同的内存单元</span><br style="line-height:25px"><span style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><wbr style="line-height:25px">static_cast</wbr></span></span><wbr style="line-height:25px"><br style="line-height:25px"> 用法:<span style="color:#0000ff; line-height:25px">static_cast&lt;type-id&gt;(expression)</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性</span>。它主要有如下几种用法:<br style="line-height:25px"><span style="color:#000080; line-height:25px">①用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换。<br style="line-height:25px"> 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;<br style="line-height:25px"> 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。<br style="line-height:25px"> ②用于基本数据类型之间的转换,如把int转换成char,把int转换成enum。这种转换的安全性也要开发人员来保证。<br style="line-height:25px"> ③把空指针转换成目标类型的空指针。<br style="line-height:25px"> ④把任何类型的表达式转换成void类型。</span><br style="line-height:25px"> 注意:<span style="color:#ff9900; line-height:25px">static_cast</span>不能转换掉expression的const、volitale、或者__unaligned属性。<br style="line-height:25px"> C++中<span style="color:#ff6600; line-height:25px">static_cast</span>和reinterpret_cast的区别<br style="line-height:25px"> C++primer第五章里写了编译器隐式执行任何类型转换都可由<span style="color:#ff6600; line-height:25px">static_cast</span>显示完成;<span style="color:#ff9900; line-height:25px">reinterpret_cast</span>通常为操作数的位模式提供较低层的重新解释<br style="line-height:25px"> 1、<span style="color:#000080; line-height:25px">C++中的static_cast执行非多态的转换,用于代替C中通常的转换操作。因此,被做为隐式类型转换使用</span>。比如:<br style="line-height:25px"> inti;<br style="line-height:25px"> floatf=166.7f;<br style="line-height:25px"> i=static_cast&lt;int&gt;(f);<br style="line-height:25px"> 此时结果,i的值为166。<br style="line-height:25px"> 2、<span style="color:#000080; line-height:25px">C++中的reinterpret_cast主要是将数据从一种类型的转换为另一种类型。所谓“通常为操作数的位模式提供较低层的重新解释”<br style="line-height:25px"> 也就是说将数据以二进制存在形式的重新解释</span>。比如:<br style="line-height:25px"> inti;<br style="line-height:25px"> char*p="Thisisaexample.";<br style="line-height:25px"> i=reinterpret_cast&lt;int&gt;(p);<br style="line-height:25px"><span style="color:#000080; line-height:25px">此时结果,i与p的值是完全相同的。reinterpret_cast的作用是说将指针p的值以二进制(位模式)的方式被解释为整型,<br style="line-height:25px"> 并赋给i,//i也是指针,整型指针;一个明显的现象是在转换前后没有数位损失</span>。<br style="line-height:25px"><span style="line-height:25px"><span style="color:#ff00ff; line-height:25px"><wbr style="line-height:25px">dynamic_cast</wbr></span></span><wbr style="line-height:25px"><br style="line-height:25px"> 用法:<span style="color:#0000ff; line-height:25px">dynamic_cast&lt;type-id&gt;(expression)</span><br style="line-height:25px"><span style="color:#003366; line-height:25px">该运算符把expression转换成type-id类型的对象。Type-id必须是类的指针、类的引用或者void*;<br style="line-height:25px"> 如果type-id是类指针类型,那么expression也必须是一个指针,如果type-id是一个引用,那么expression也必须是一个引用。<br style="line-height:25px"> dynamic_cast主要用于类层次间的上行转换和下行转换,还可以用于类之间的交叉转换。<br style="line-height:25px"> 在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的; 在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全</span>。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">B</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> public:<br style="line-height:25px"> intm_iNum;<br style="line-height:25px"> virtualvoidfoo();<br style="line-height:25px"> };<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">D</span><span style="color:#3366ff; line-height:25px">:</span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">B{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">public:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">char*m_szName[100];</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">void</span><span style="color:#3366ff; line-height:25px">func(B*pb){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">D*pd1=static_cast&lt;D*&gt;(pb);<br style="line-height:25px"> D*pd2=dynamic_cast&lt;D*&gt;(pb);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 在上面的代码段中,如果pb指向一个D类型的对象,pd1和pd2是一样的,并且对这两个指针执行D类型的任何操作都是安全的;<br style="line-height:25px"> 但是,如果pb指向的是一个B类型的对象,那么pd1将是一个指向该对象的指针,对它进行D类型的操作将是不安全的(如访问m_szName),<br style="line-height:25px"> 而pd2将是一个空指针。<br style="line-height:25px"> 注意:B要有虚函数,否则会编译出错;static_cast则没有这个限制。<br style="line-height:25px"><br style="line-height:25px"><span style="color:#003366; line-height:25px"><wbr style="line-height:25px">作为四个内部类型转换操作符之一的</wbr></span><span style="color:#ff00ff; line-height:25px">dynamic_cast</span><span style="color:#003366; line-height:25px">和传统的C风格的强制类型转换有着巨大的差别。除了</span><span style="color:#ff00ff; line-height:25px">dynamic_cast</span><span style="color:#003366; line-height:25px">以外的转换,<br style="line-height:25px"> 其行为的都是在编译期就得以确定的,转换是否成功,并不依赖被转换的对象。而</span><span style="color:#ff00ff; line-height:25px">dynamic_cast</span><span style="color:#003366; line-height:25px">则不然<wbr style="line-height:25px">。<br style="line-height:25px"><wbr style="line-height:25px">首先,</wbr></wbr></span><span style="color:#ff6600; line-height:25px">dynamic_cast</span><span style="color:#003366; line-height:25px">依赖于RTTI信息</span><wbr style="line-height:25px">,其次,在转换时,<span style="color:#ff6600; line-height:25px">dynamic_cast</span><span style="color:#000080; line-height:25px">会检查转换的source对象是否真的可以转换成target类型,这种检查不是语法上的,而是真实情况的检查。先看</span><span style="color:#0000ff; line-height:25px">RTTI</span><span style="color:#000080; line-height:25px">相关部分,通常,许多编译器都是通过vtable找到对象的RTTI信息的,<br style="line-height:25px"> 这也就意味着,如果基类没有虚方法,也就无法判断一个基类指针变量所指对象的真实类型,<br style="line-height:25px"> 这时候,</span><span style="color:#ff6600; line-height:25px">dynamic_cast</span><span style="color:#000080; line-height:25px">只能用来做安全的转换,例如从派生类指针转换成基类指针.而这种转换其实并不需要dynamic_cast参与.也就是说,</span><span style="color:#ff6600; line-height:25px">dynamic_cas</span><span style="color:#000080; line-height:25px">t是根据RTTI记载的信息来判断类型转换是否合法的.</span><br style="line-height:25px"> 另外,<wbr style="line-height:25px"><span style="color:#ff00ff; line-height:25px">dynamic_cast</span>还支持交叉转换(crosscast)<wbr style="line-height:25px">。如下代码所示。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">A{<br style="line-height:25px"> public:<br style="line-height:25px"> intm_iNum;<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">virtual</span><span style="color:#3366ff; line-height:25px">voidf(){}<br style="line-height:25px"> };<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">B:publicA{<br style="line-height:25px"> };<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">class</span><span style="color:#3366ff; line-height:25px">D:publicA{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">voidfoo(){</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">B*pb=newB;<br style="line-height:25px"> pb-&gt;m_iNum=100;<br style="line-height:25px"> D*pd1=static_cast&lt;D*&gt;(pb);</span><span style="color:#808080; line-height:25px">//compileerror</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">D*pd2=dynamic_cast&lt;D*&gt;(pb);</span><span style="color:#808080; line-height:25px">//pd2isNULL</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">deletepb;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#000080; line-height:25px"></span><wbr style="line-height:25px">在函数foo中,使用<span style="color:#ff9900; line-height:25px">static_cast</span>进行转换是不被允许的,将在编译时出错;而使用<span style="color:#ff6600; line-height:25px">dynamic_cast</span>的转换则是允许的,结果是空指针<wbr style="line-height:25px">。<br style="line-height:25px"> 下面再看一个例子:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">struct</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">B1</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">virtual</span><span style="color:#3366ff; line-height:25px">~B1(){}<br style="line-height:25px"> };<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">struct</span><span style="color:#ff6600; line-height:25px">B2</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">virtual</span><span style="color:#3366ff; line-height:25px">~B2(){}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">structD1:B1,B2{};</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">intmain()</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">D1d;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">B1*pb1=&amp;d;</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">B2*pb2=dynamic_cast&lt;B2*&gt;(pb1);</span><span style="color:#666699; line-height:25px">//L1</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">B2*pb22=static_cast&lt;B2*&gt;(pb1);</span><span style="color:#808080; line-height:25px">//L2</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">0;<br style="line-height:25px"> }</span><br style="line-height:25px"> 上述定义中可以看到,B1和B2是不相关的类,从L1可以看到,<span style="color:#ff6600; line-height:25px">dynamic_cast</span>允许这种转换:只要B1存在多态方法.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px"></wbr></span>L2将编译失败,<span style="color:#ff6600; line-height:25px">static_cast</span>并不允许两个完全不相干的类互相转换.<wbr style="line-height:25px"><br style="line-height:25px"> 困惑:http://zhidao.baidu.com/question/56811956.html</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值