`
kanwoerzi
  • 浏览: 1643097 次
文章分类
社区版块
存档分类
最新评论

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>
分享到:
评论

相关推荐

    细说C++类型转换操作符

    C++语言虽然兼容C,也支持C语言的类型转换用法,是将类型用圆括号括起来,这种转换可视性比较差,难以跟踪错误的转换。C++提供了命名的强制转换操作符,毕竟转换有时是不安全的,所以使用命名转换操作符可以根据所...

    浅谈C++中强制类型转换函数

    C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。

    有关C++中类类型转换操作符总结(必看篇)

    下面小编就为大家带来一篇有关C++中类类型转换操作符总结(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    C++中的四种类型转换

    总所周知,在C++ 当中引入了四种新的类型转换操作符:static_cast, dynamic_cast, reinterpret_cast,还有const_cast。就自己见过的一些C++代码当中,它们的使用其实并不普遍。不少程序员依然乐于去使用C-like的类型...

    C++的类型转换详细介绍

     C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。  static_cast 静态类型转换。如int转换成char  reinterpreter_cast 重新解释类型  dynamic_cast 命 名上理解是动态类型转换。如子类和父类...

    C++操作符重载的变态用途之子类转换

    本文给大家分享了C++操作符重载的变态用途之子类转换。

    77G 22套C语言 C++ 数据结构 程序设计视频课程合集 C丨C++相关学习视频全套视频教程

    VC++ MFC 经典教程 - 基础篇 1.CP_YourFirstWindowsProgram.mp4 10.MFC_GDI_画直线和曲线.mp4 11.MFC_GDI_画椭圆_多边形及其他形状.mp4 ...重载赋值操作符_3.mp4 46.自动类型转换.mp4 47.动态创建对象...

    C++类型转换的深入总结

    C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用。 const_cast,字面上理解就是去const属性。 static_cast,命名上理解是静态类型转换。如int转换成char。 dynamic_cast,命名上理解是动态类型转换。...

    C++语言中的四种类型转换

     总所周知,在C++ 当中引入了四种新的类型转换操作符:static_cast, dynamic_cast, reinterpret_cast,还有const_cast。自己见过的一些C++代码当中,它们的使用其实并不普遍。不少程序员依然乐于去使用C-like的...

    C++四种cast操作符

    C 风格(C-style)强制转型如下...ANSI-C++标准定义了四个新的转换符:reinterpret_cast, static_cast, dynamic_cast和const_cast,目的在于控制类(class)之间的类型转换。  1.1 reinpreter_cast  用法:reinpre

    C++中的4种类型转换方式

    类型转换有c风格的,当然还有c++风格的。c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c...所以c++为了克服这些缺点,引进了4新的类型转换操作符,他们是1.static_cast 

    C# To C++ Converter 17.10.2 (最新破解版 )

    C# To C++ Converter是一款将C#代码或者项目转换为C++的工具 转换所有版本的C#代码 ...操作符重载从C#到C ++的精确转换 评估所有引用的程序集和.NET项目,以便更完整地解析外部引用 准确甚至格式不正确的C#代码

    C++中4种类型转换方式 cast操作详解

    为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符。比如,为了转换一个类型为doubole的浮点数的指针到整型:代码:inti;doubled; i=(int)d;或者: i=int(d); 对于具有标准定义转换的简单类型而言...

    C++ Primer第四版【中文高清扫描版】.pdf

    14.9.2 转换操作符 455 14.9.3 实参匹配和转换 458 14.9.4 重载确定和类的实参 461 14.9.5 重载、转换和操作符 464 小结 466 术语 467 第四部分 面向对象编程与泛型编程 第15章 面向对象编程 471 15.1 面向对象编程...

    基于c++强制类型转换的(总结)详解

    为了类型转换一个简单对象为另一个对象你会使用传统的类型转换操作符。 C与C++的类型转换 C中: 代码如下:(T)element 或者 T(element)c++中: 代码如下:reinterpret_cast&lt;T&gt; (expression)dynamic_cast ...

    非常经典的c++ primer视频教程6-10

    5.2 关系操作符和逻辑操作符 5.3 位操作符 5.3.1 bitset对象或整型值的使用 5.3.2 将移位操作符用于IO 5.4 赋值操作符 5.4.1 赋值操作的右结合性 5.4.2 赋值操作具有低优先级 5.4.3 复合赋值操作符 5.5 自增...

    C++基础教程完整版

    对于C语言熟悉的读者可将前三章(1.1 到 3.4)当作复习,因为这部分内容主要介绍C++中的C部分。不过某些C++的语法与C还是有些差别,所以建议还是快速的读一下这部分。 第四章讲述面向对象编程。 第五章主要介绍ANSI-...

    C语言解析教程(原书第4版)(美) 凯利.pdf

    第2章 词法元素、操作符和c系统 2.1 字符和词法元素 2.2 语法规则 2.3 注释 2.4 关键字 2.5 标识符 2.6 常量 2.7 字符串常量 2.8 操作符和标点符号 2.9 操作符的优先级和结合性 2.10 增值操作符和减值操作符 2.11 ...

    C++ primer 第4版 原书+习题解答+源码 清晰pdf

     第14章 重载操作符与转换  第四部分 面向对象编程与泛型编程  第15章 面向对象编程  第16章 模板与泛型编程  第五部分 高级主题  第17章 用于大型程序的工具  第18章 特殊工具与技术  附录 ...

    传智播客C语言课件

    2.C语言运算符,C语言操作符,C语言表达式,表达式优先级 3.C语言流程控制,分支,顺序,循环逻辑控制 4.函数概述与引用,简单递归,简单嵌套,C语言调用函数示例 5.数组,二维数组,高维数组,详细案例 6.指针,...

Global site tag (gtag.js) - Google Analytics