﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-@流年-随笔分类-C++</title><link>http://www.cppblog.com/palm/category/16186.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 07 Mar 2011 06:42:38 GMT</lastBuildDate><pubDate>Mon, 07 Mar 2011 06:42:38 GMT</pubDate><ttl>60</ttl><item><title>C++风格的类型转换</title><link>http://www.cppblog.com/palm/archive/2011/03/07/141272.html</link><dc:creator>郭小帅</dc:creator><author>郭小帅</author><pubDate>Mon, 07 Mar 2011 06:28:00 GMT</pubDate><guid>http://www.cppblog.com/palm/archive/2011/03/07/141272.html</guid><wfw:comment>http://www.cppblog.com/palm/comments/141272.html</wfw:comment><comments>http://www.cppblog.com/palm/archive/2011/03/07/141272.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/palm/comments/commentRss/141272.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/palm/services/trackbacks/141272.html</trackback:ping><description><![CDATA[
C++有四种类型转换操作符：static_cast、const_cast、dynamic_cast、reinterpret_cast。<div>1&gt;. static_cast普通转换，如double转int：</div><div><table id="Table1" class="tb" cellspacing="0" cellpadding="3" border="0" style="font-size: 13px; "><tbody><tr><td><pre><div><span style="color: rgb(0, 128, 128); ">1</span> <span style="color: rgb(0, 0, 255); ">double</span><span style="color: rgb(0, 0, 0); "> a </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">2</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> b </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> static_cast</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">(a);</span></div></pre></td></tr></tbody></table></div><div>2&gt;. const_cast改变const或者volatile属性:</div><div><span style="font-size: 13px; "><pre><div><span style="color: rgb(0, 128, 128); ">1</span> <span style="color: rgb(0, 0, 255); ">const</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); "> szA </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">test</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">;
</span><span style="color: rgb(0, 128, 128); ">2</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); "> szB </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> const_cast</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 255); ">char</span><span style="color: rgb(0, 0, 0); ">*&gt;</span><span style="color: rgb(0, 0, 0); ">(szA);</span></div></pre></span></div><div>3&gt;. dynamic_cast把指向基类的指针或引用转换成指向派生类或者基类的兄弟类的指针或引用：</div><div><table id="Table1" class="tb" cellspacing="0" cellpadding="3" border="0" style="font-size: 13px; "><tbody><tr><td><pre><div><span style="color: rgb(0, 128, 128); "> 1</span> <span style="color: rgb(0, 0, 255); ">class</span><span style="color: rgb(0, 0, 0); "> Base
</span><span style="color: rgb(0, 128, 128); "> 2</span> <span style="color: rgb(0, 0, 0); ">{
</span><span style="color: rgb(0, 128, 128); "> 3</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">public</span><span style="color: rgb(0, 0, 0); ">:
</span><span style="color: rgb(0, 128, 128); "> 4</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">virtual</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> test(){} </span><span style="color: rgb(0, 128, 0); ">//</span><span style="color: rgb(0, 128, 0); ">基类必须有虚函数</span><span style="color: rgb(0, 128, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 5</span> <span style="color: rgb(0, 128, 0); "></span><span style="color: rgb(0, 0, 0); ">};
</span><span style="color: rgb(0, 128, 128); "> 6</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); "> 7</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">class</span><span style="color: rgb(0, 0, 0); "> Child : </span><span style="color: rgb(0, 0, 255); ">public</span><span style="color: rgb(0, 0, 0); "> Base
</span><span style="color: rgb(0, 128, 128); "> 8</span> <span style="color: rgb(0, 0, 0); ">{
</span><span style="color: rgb(0, 128, 128); "> 9</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">public</span><span style="color: rgb(0, 0, 0); ">:
</span><span style="color: rgb(0, 128, 128); ">10</span> <span style="color: rgb(0, 0, 0); ">    </span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> print(){ cout </span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); ">child::base</span><span style="color: rgb(0, 0, 0); ">"</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">&lt;&lt;</span><span style="color: rgb(0, 0, 0); "> endl; }
</span><span style="color: rgb(0, 128, 128); ">11</span> <span style="color: rgb(0, 0, 0); ">};
</span><span style="color: rgb(0, 128, 128); ">12</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">13</span> <span style="color: rgb(0, 0, 0); ">Base</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); "> pBase </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 255); ">new</span><span style="color: rgb(0, 0, 0); "> Base();
</span><span style="color: rgb(0, 128, 128); ">14</span> <span style="color: rgb(0, 0, 0); ">Child</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); "> pChild </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> dynamic_cast</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">Child</span><span style="color: rgb(0, 0, 0); ">*&gt;</span><span style="color: rgb(0, 0, 0); ">(pBase);
</span><span style="color: rgb(0, 128, 128); ">15</span> <span style="color: rgb(0, 0, 0); ">pChild</span><span style="color: rgb(0, 0, 0); ">-&gt;</span><span style="color: rgb(0, 0, 0); ">print();</span></div></pre></td></tr></tbody></table></div><div>4&gt;. reinterpret_cast用来在函数指针之间进行类型转换</div><div><font face="monospace" size="2"><span style="white-space: pre;"><span style="font-family: Simsun; white-space: normal; font-size: 13px; "><pre><div><span style="color: rgb(0, 128, 128); ">1</span> <span style="color: rgb(0, 0, 0); ">typedef </span><span style="color: rgb(0, 0, 255); ">void</span><span style="color: rgb(0, 0, 0); "> (</span><span style="color: rgb(0, 0, 0); ">*</span><span style="color: rgb(0, 0, 0); ">FuncPtr)();
</span><span style="color: rgb(0, 128, 128); ">2</span> <span style="color: rgb(0, 0, 0); ">FuncPtr funcPtrArray[</span><span style="color: rgb(0, 0, 0); ">10</span><span style="color: rgb(0, 0, 0); ">];
</span><span style="color: rgb(0, 128, 128); ">3</span> <span style="color: rgb(0, 0, 0); "></span><span style="color: rgb(0, 0, 255); ">int</span><span style="color: rgb(0, 0, 0); "> doSomeing(){</span><span style="color: rgb(0, 0, 255); ">return</span><span style="color: rgb(0, 0, 0); "> </span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">;}
</span><span style="color: rgb(0, 128, 128); ">4</span> <span style="color: rgb(0, 0, 0); ">
</span><span style="color: rgb(0, 128, 128); ">5</span> <span style="color: rgb(0, 0, 0); ">funcPtrArray[</span><span style="color: rgb(0, 0, 0); ">0</span><span style="color: rgb(0, 0, 0); ">] </span><span style="color: rgb(0, 0, 0); ">=</span><span style="color: rgb(0, 0, 0); "> reinterpret_cast</span><span style="color: rgb(0, 0, 0); ">&lt;</span><span style="color: rgb(0, 0, 0); ">FuncPtr</span><span style="color: rgb(0, 0, 0); ">&gt;</span><span style="color: rgb(0, 0, 0); ">(</span><span style="color: rgb(0, 0, 0); ">&amp;</span><span style="color: rgb(0, 0, 0); ">doSomeing);</span></div></pre></span></span></font></div><img src ="http://www.cppblog.com/palm/aggbug/141272.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/palm/" target="_blank">郭小帅</a> 2011-03-07 14:28 <a href="http://www.cppblog.com/palm/archive/2011/03/07/141272.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>