﻿<?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++博客-jokes000-随笔分类-C++ Notes</title><link>http://www.cppblog.com/jokes000/category/17894.html</link><description /><language>zh-cn</language><lastBuildDate>Sun, 16 Oct 2011 13:46:55 GMT</lastBuildDate><pubDate>Sun, 16 Oct 2011 13:46:55 GMT</pubDate><ttl>60</ttl><item><title>虚析构函数</title><link>http://www.cppblog.com/jokes000/archive/2011/10/16/158478.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Sun, 16 Oct 2011 10:39:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/16/158478.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/158478.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/16/158478.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/158478.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/158478.html</trackback:ping><description><![CDATA[&nbsp; &nbsp; &nbsp; 自动调用基类部分的西沟函数对基类的设计有重要影响。<br /><br />&nbsp; &nbsp; &nbsp; 删除指向动态分配对象的指针时，需要运行西沟函数在释放对象的内存之前清楚对象。处理继承层次中的对象时，指针的静态类型可能与被删除对象的动态类型不同，可能会删除实际指向派生类对象的基类类型指针。<br />&nbsp; &nbsp; &nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果删除基类指针，则需要运行积累西沟函数并清楚基类成员，如果对象实际是派生类型的，则没有定义该行为。要保证运行适当的析构函数，基类中的析构函数必须为虚函数：<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_183918" onclick="this.style.display='none'; Code_Closed_Text_183918.style.display='none'; Code_Open_Image_183918.style.display='inline'; Code_Open_Text_183918.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_183918" style="display: none" onclick="this.style.display='none'; Code_Open_Text_183918.style.display='none'; Code_Closed_Image_183918.style.display='inline'; Code_Closed_Text_183918.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_183918" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">Code</span><span id="Code_Open_Text_183918" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;item_base<br />{<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;no&nbsp;work,&nbsp;but&nbsp;virtual&nbsp;destructor&nbsp;needed<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;if&nbsp;base&nbsp;pointer&nbsp;that&nbsp;points&nbsp;to&nbsp;a&nbsp;derived&nbsp;object&nbsp;is&nbsp;ever&nbsp;deleted</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">virtual</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">~</span><span style="color: #000000; ">item_base()&nbsp;{}<br />};<br /><br />item_base&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">itemP&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;item_base;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;same&nbsp;static&nbsp;and&nbsp;dynamic&nbsp;type</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">delete&nbsp;itemP;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;ok:&nbsp;destructor&nbsp;for&nbsp;item_base&nbsp;called</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">itemP&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">new</span><span style="color: #000000; ">&nbsp;Bulk_item;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;ok:&nbsp;static&nbsp;and&nbsp;dynamic&nbsp;types&nbsp;differ</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">delete&nbsp;itemP;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; "></span></span></div><img src ="http://www.cppblog.com/jokes000/aggbug/158478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-16 18:39 <a href="http://www.cppblog.com/jokes000/archive/2011/10/16/158478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>优先使用C++风格类型转换</title><link>http://www.cppblog.com/jokes000/archive/2011/10/13/158235.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Thu, 13 Oct 2011 09:06:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/13/158235.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/158235.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/13/158235.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/158235.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/158235.html</trackback:ping><description><![CDATA[&nbsp; &nbsp; &nbsp; A. C风格类型转换就像粗鲁的野兽，可以通过它们在任意类型之间进行转换。如果能够更明确地指定每个转换的目的，可能会更好。<br />&nbsp; &nbsp; &nbsp; B. C风格类型转换很难进行查找。<br /><br />&nbsp; &nbsp; &nbsp; C++通过引入4种新的类型转换操作符客服了C风格的类型转换的缺点。这4种操作符是：<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.static_cast: 与通用的C风格类型转换，基本上有着同样地能力和意义。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_165946" onclick="this.style.display='none'; Code_Closed_Text_165946.style.display='none'; Code_Open_Image_165946.style.display='inline'; Code_Open_Text_165946.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_165946" style="display: none" onclick="this.style.display='none'; Code_Open_Text_165946.style.display='none'; Code_Closed_Image_165946.style.display='inline'; Code_Closed_Text_165946.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_165946" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">static_cast</span><span id="Code_Open_Text_165946" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;firstNum,secondNum;<br /></span><span style="color: #0000FF; ">double</span><span style="color: #000000; ">&nbsp;result&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;((</span><span style="color: #0000FF; ">double</span><span style="color: #000000; ">)firstNum)</span><span style="color: #000000; ">/</span><span style="color: #000000; ">secondNum;<br /></span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;使用新的类型转换，应该这样写：</span><span style="color: #008000; "><br /></span><span style="color: #0000FF; ">double</span><span style="color: #000000; ">&nbsp;result&nbsp;</span><span style="color: #000000; ">=</span><span style="color: #000000; ">&nbsp;static_cast</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">double</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">(firstNum)</span><span style="color: #000000; ">/</span><span style="color: #000000; "></span></span></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2.const_cast: 最常用法，去除掉一个对象的const属性。<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3.dynamic_cast: 用来针对一个继承体系做向下或者横向的安全转换。<br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><img id="Code_Closed_Image_170242" onclick="this.style.display='none'; Code_Closed_Text_170242.style.display='none'; Code_Open_Image_170242.style.display='inline'; Code_Open_Text_170242.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_170242" style="display: none" onclick="this.style.display='none'; Code_Open_Text_170242.style.display='none'; Code_Closed_Image_170242.style.display='inline'; Code_Closed_Text_170242.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_170242" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">dynamic_cast</span><span id="Code_Open_Text_170242" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;SpecialWidget继承于Widget</span><span style="color: #008000; "><br /></span><span style="color: #000000; ">Widget&nbsp;</span><span style="color: #000000; ">*</span><span style="color: #000000; ">pw;<br />update(&nbsp;dynamic_cast</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">SpecialWidget</span><span style="color: #000000; ">*&gt;</span><span style="color: #000000; ">(pw)&nbsp;);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;fine,&nbsp;passes&nbsp;to&nbsp;update&nbsp;a&nbsp;pointer<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;to&nbsp;the&nbsp;SpecialWidget&nbsp;pw&nbsp;points&nbsp;to<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; ">&nbsp;if&nbsp;pw&nbsp;really&nbsp;points&nbsp;to&nbsp;one,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000; ">//</span><span style="color: #008000; "></span></span></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.reinterpret_cast: 最常见用法是在函数指针之间进行类型转换。<img src ="http://www.cppblog.com/jokes000/aggbug/158235.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-13 17:06 <a href="http://www.cppblog.com/jokes000/archive/2011/10/13/158235.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>区分指针和引用</title><link>http://www.cppblog.com/jokes000/archive/2011/10/12/158163.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Wed, 12 Oct 2011 13:20:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/12/158163.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/158163.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/12/158163.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/158163.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/158163.html</trackback:ping><description><![CDATA[摘自：《More Effective C++》<br /><br />&nbsp; &nbsp; &nbsp; 指针和引用看上去非常不同（指针使用&#8220;*&#8221;和&#8220;-&gt;"操作符，引用使用的是&#8221;.&#8220;），但是它们所做的事情似乎很类似。指针和引用都可以间接地指代其他对象。那么，如何决定什么时候使用其中一个而不是另外一个？<br /><br />&nbsp; &nbsp; &nbsp; 首先，要认识到不存在类似于空引用（null reference）这样的东西。<span style="color: red">引用必须指代某个对象。</span>因此，如果有一个用于指代另一个对象的变量，但是有可能没有这么一个可供指代的对象，这时就应该把这个变量声明成指针，因为这样就可以把它设为空值(null)。反之，如果这个变量必须指代一个对象，也就是说，不允许这个变量为空值，那么你基本上应该把这个变量声明成引用。<br /><br />&nbsp; &nbsp; &nbsp; 不存在空引用这个事实意味着，使用引用比使用指针更搞笑。因为在使用引用之前不需要测试是否有效。 
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img id="Code_Closed_Image_152732" onclick="this.style.display='none'; Code_Closed_Text_152732.style.display='none'; Code_Open_Image_152732.style.display='inline'; Code_Open_Text_152732.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_152732" style="display: none" onclick="this.style.display='none'; Code_Open_Text_152732.style.display='none'; Code_Closed_Image_152732.style.display='inline'; Code_Closed_Text_152732.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_152732" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">different</span><span id="Code_Open_Text_152732" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;printDouble(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;rd)<br /><img id="Codehighlighter1_35_110_Open_Image" onclick="this.style.display='none'; Codehighlighter1_35_110_Open_Text.style.display='none'; Codehighlighter1_35_110_Closed_Image.style.display='inline'; Codehighlighter1_35_110_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_35_110_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_35_110_Closed_Text.style.display='none'; Codehighlighter1_35_110_Open_Image.style.display='inline'; Codehighlighter1_35_110_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_35_110_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_35_110_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;rd;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;no&nbsp;need&nbsp;to&nbsp;test&nbsp;rd;&nbsp;it<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;must&nbsp;refer&nbsp;to&nbsp;a&nbsp;double</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" /></span><span style="color: #000000">}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;printDouble(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;</span><span style="color: #0000ff">double</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">rd)<br /><img id="Codehighlighter1_148_205_Open_Image" onclick="this.style.display='none'; Codehighlighter1_148_205_Open_Text.style.display='none'; Codehighlighter1_148_205_Closed_Image.style.display='inline'; Codehighlighter1_148_205_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top"><img id="Codehighlighter1_148_205_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_148_205_Closed_Text.style.display='none'; Codehighlighter1_148_205_Open_Image.style.display='inline'; Codehighlighter1_148_205_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top"></span><span id="Codehighlighter1_148_205_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_148_205_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">if</span><span style="color: #000000">(rd)<br /><img id="Codehighlighter1_159_203_Open_Image" onclick="this.style.display='none'; Codehighlighter1_159_203_Open_Text.style.display='none'; Codehighlighter1_159_203_Closed_Image.style.display='inline'; Codehighlighter1_159_203_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top"><img id="Codehighlighter1_159_203_Closed_Image" style="display: none" onclick="this.style.display='none'; Codehighlighter1_159_203_Closed_Text.style.display='none'; Codehighlighter1_159_203_Open_Image.style.display='inline'; Codehighlighter1_159_203_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;</span><span id="Codehighlighter1_159_203_Closed_Text" style="border-right: #808080 1px solid; border-top: #808080 1px solid; display: none; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_159_203_Open_Text"><span style="color: #000000">{<br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">rd;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;check&nbsp;for&nbsp;null&nbsp;pointer</span><span style="color: #008000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /><img alt="" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span></span></span></div><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 另一个重要不同之处：指针可以被重新赋值用以指向另外一个不同的对象；而引用则总是指向初始化时它所指代的对象。<br />
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img id="Code_Closed_Image_210448" onclick="this.style.display='none'; Code_Closed_Text_210448.style.display='none'; Code_Open_Image_210448.style.display='inline'; Code_Open_Text_210448.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_210448" style="display: none" onclick="this.style.display='none'; Code_Open_Text_210448.style.display='none'; Code_Closed_Image_210448.style.display='inline'; Code_Closed_Text_210448.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_210448" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">Another Difference</span><span id="Code_Open_Text_210448" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;s1(</span><span style="color: #000000">"</span><span style="color: #000000">Nancy</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;s2(</span><span style="color: #000000">"</span><span style="color: #000000">Clancy</span><span style="color: #000000">"</span><span style="color: #000000">);<br /><br /></span><span style="color: #0000ff">string</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;rs&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s1;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;rs&nbsp;refers&nbsp;to&nbsp;s1</span><span style="color: #008000"><br /></span><span style="color: #0000ff">string</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">ps&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">s1;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;ps&nbsp;points&nbsp;to&nbsp;s1</span><span style="color: #008000"><br /></span><span style="color: #000000"><br />rs&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;s2;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;rs&nbsp;still&nbsp;refers&nbsp;to&nbsp;s1,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;but&nbsp;s1's&nbsp;value&nbsp;is&nbsp;now<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;"Clancy"</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />ps&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&amp;</span><span style="color: #000000">s2&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;ps&nbsp;now&nbsp;points&nbsp;to&nbsp;s2;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000"></span></span></div><br />&nbsp; &nbsp; &nbsp; 总而言之，当需要考虑以下两种情况的时候，你应该使用指针：<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A.有可能什么也不指向（这种情况下，你可以把指针设为null）；<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B.需要能够在不同时候指向不同的对象。<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当你知道总是会指向某一个对象并且还知道一旦指向那个对象，就不会再指向其他对象的时候，应该使用引用。<br />&nbsp; &nbsp; &nbsp; 还有一种情况应该使用引用，即当实现某些操作符的时候。最常见的例子是[ ]操作符。这个操作符通常需要返回一个值作为(下一个)赋值语句的赋值目标。<br />
<div style="border-right: #cccccc 1px solid; padding-right: 5px; border-top: #cccccc 1px solid; padding-left: 4px; font-size: 13px; padding-bottom: 4px; border-left: #cccccc 1px solid; width: 98%; word-break: break-all; padding-top: 4px; border-bottom: #cccccc 1px solid; background-color: #eeeeee"><img id="Code_Closed_Image_211738" onclick="this.style.display='none'; Code_Closed_Text_211738.style.display='none'; Code_Open_Image_211738.style.display='inline'; Code_Open_Text_211738.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" width="11" align="top"><img id="Code_Open_Image_211738" style="display: none" onclick="this.style.display='none'; Code_Open_Text_211738.style.display='none'; Code_Closed_Image_211738.style.display='inline'; Code_Closed_Text_211738.style.display='inline';" height="16" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" width="11" align="top"><span id="Code_Closed_Text_211738" style="border-right: #808080 1px solid; border-top: #808080 1px solid; border-left: #808080 1px solid; border-bottom: #808080 1px solid; background-color: #ffffff">Difference</span><span id="Code_Open_Text_211738" style="display: none"><br /><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000">vector</span><span style="color: #000000">&lt;</span><span style="color: #0000ff">int</span><span style="color: #000000">&gt;</span><span style="color: #000000">&nbsp;v(</span><span style="color: #000000">10</span><span style="color: #000000">);&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;create&nbsp;an&nbsp;int&nbsp;vector&nbsp;of&nbsp;size&nbsp;10;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;vector&nbsp;is&nbsp;a&nbsp;template&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;standard&nbsp;C++&nbsp;library&nbsp;(see&nbsp;Item&nbsp;35)</span><span style="color: #008000"><br /></span><span style="color: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />v[</span><span style="color: #000000">5</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;the&nbsp;target&nbsp;of&nbsp;this&nbsp;assignment&nbsp;is<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;operator[]<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;如果[&nbsp;]操作符返回一个指针，上面最后一个语句就必须写成：</span><span style="color: #008000"><br /></span><span style="color: #000000">&#215;v[</span><span style="color: #000000">5</span><span style="color: #000000">]&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">10</span><span style="color: #000000">;<br /><br /></span><span style="color: #008000">//</span><span style="color: #008000">&nbsp;但是这让v看起来像是一个关于指针的向量(vector)，而实际上并非如此。<br /></span><span style="color: #008000">//</span><span style="color: #008000"></span></span></div><br />&nbsp; &nbsp; &nbsp; 当你知道要指代某个对象并且不会再指代其他的东西时，那就应该选择引用。当实现某些操作符的时候，如果这些操作符在语义上的要求使得指针不可行，此时仍然需要使用引用。其他情况下，应该使用指针。<br /><br /><img src ="http://www.cppblog.com/jokes000/aggbug/158163.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-12 21:20 <a href="http://www.cppblog.com/jokes000/archive/2011/10/12/158163.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++ 函数名字查找与继承基础知识讲解</title><link>http://www.cppblog.com/jokes000/archive/2011/10/12/158129.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Wed, 12 Oct 2011 06:56:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/12/158129.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/158129.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/12/158129.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/158129.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/158129.html</trackback:ping><description><![CDATA[&nbsp; &nbsp; &nbsp; 理解C++中继承层次的关键在于理解如何确定函数调用。确定函数调用遵循以下四个步骤：<br />&nbsp; &nbsp; &nbsp; A.首先确定进行函数调用的对象、引用或指针的静态类型；<br />&nbsp; &nbsp; &nbsp; B.在该类中查找函数，如果找不到，就在直接基类中查找，如此循着类的继承链网上找，知道找到该函数或者查找完最后一个类。如果不能在类或其相关基类中找到该名字，则调用是错误的。<br />&nbsp; &nbsp; &nbsp; C.一旦找到了该名字，就进行常规类型检查，查看如果给定找到的定义，该函数调用是否合法。<br />&nbsp; &nbsp; &nbsp; D.假定函数调用合法，编译器就生成代码。如果函数是虚函数且通过引用或指针调用，则编译器生成代码以确定根据对象的动态类型运行哪个版本函数，否则，编译器生成代码直接调用函数。<img src ="http://www.cppblog.com/jokes000/aggbug/158129.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-12 14:56 <a href="http://www.cppblog.com/jokes000/archive/2011/10/12/158129.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++接口的定义用一个实例说明</title><link>http://www.cppblog.com/jokes000/archive/2011/10/11/158083.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Tue, 11 Oct 2011 13:57:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/11/158083.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/158083.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/11/158083.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/158083.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/158083.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="color: #625b49; font-family: Arial; line-height: normal; background-color: #ffffff; ">转自：<a href="http://xujingli88.blog.163.com/blog/static/41178619200962410122172/" style="font-size: 12px; color: #625b49; text-decoration: underline; line-height: normal; ">http://xujingli88.blog.163.com/blog/static/41178619200962410122172/</a></span><span class="Apple-style-span" style="color: #625b49; font-family: Arial; line-height: 20px; background-color: #ffffff; "><p style="line-height: normal; "><br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;接口是一个没有被实现的特殊的类，它是一系列操作的集合，我们可以把它看作是与其他对象通讯的协议。C++中没有提供类似interface这样的关键 字来定义接口，但是Mircrosoft c++中提供了__declspec(novtable)来修饰一个类，来表示该类没有虚函数表，也就是虚函数都是纯虚的。所以利用它我们依然可以定义一 个接口。代码例子如下：</p><p style="line-height: normal; ">&nbsp;</p><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><span style="line-height: normal; ">#include&nbsp;</span><span style="line-height: normal; ">&lt;</span><span style="line-height: normal; ">IOSTREAM</span><span style="line-height: normal; ">&gt;</span><span style="line-height: normal; "><br style="line-height: normal; " /></span><span style="line-height: normal; ">using</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">namespace</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;std;</span><br style="line-height: normal; " /><br style="line-height: normal; " /></span><span style="line-height: normal; ">#define</span><span style="line-height: normal; ">&nbsp;interface class __declspec(novtable)</span><span style="line-height: normal; "><br style="line-height: normal; " /><br style="line-height: normal; " /></span><span style="line-height: normal; ">interface</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;ICodec</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">{</span><br style="line-height: normal; " /></span><span style="line-height: normal; ">public</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">:</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">virtual</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">bool</span><span style="line-height: normal; ">&nbsp;Decode(</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataSrc,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;nSrcLen,</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataDst,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">pnDstLen);</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">virtual</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">bool</span><span style="line-height: normal; ">&nbsp;Encode(</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataSrc,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;nSrcLen,</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataDst,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">pnDstLen);</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">};</span><br style="line-height: normal; " /><br style="line-height: normal; " /></span><span style="line-height: normal; ">class</span><span style="line-height: normal; ">&nbsp;CCodec :&nbsp;</span><span style="line-height: normal; ">public</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;ICodec</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">{</span><br style="line-height: normal; " /></span><span style="line-height: normal; ">public</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">:</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">virtual</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">bool</span><span style="line-height: normal; ">&nbsp;Decode(</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataSrc,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;nSrcLen,</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataDst,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">pnDstLen)</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; {</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&nbsp;</span></span><span style="line-height: normal; ">&lt;&lt;</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">"</span><span style="line-height: normal; ">解码...</span><span style="line-height: normal; ">"</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">&lt;&lt;</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;endl;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">return</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">true</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; }</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">virtual</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">bool</span><span style="line-height: normal; ">&nbsp;Encode(</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataSrc,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;nSrcLen,</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;lpDataDst,unsigned&nbsp;</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">pnDstLen)</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; {</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&nbsp;</span></span><span style="line-height: normal; ">&lt;&lt;</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">"</span><span style="line-height: normal; ">编码...</span><span style="line-height: normal; ">"</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">&lt;&lt;</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;endl;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">return</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">true</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; }</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">};</span><br style="line-height: normal; " /><br style="line-height: normal; " /></span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;main(</span><span style="line-height: normal; ">int</span><span style="line-height: normal; ">&nbsp;argc,&nbsp;</span><span style="line-height: normal; ">char</span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;argv[])</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">{</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; ICodec&nbsp;</span></span><span style="line-height: normal; ">*</span><span style="line-height: normal; ">&nbsp;pCodec&nbsp;</span><span style="line-height: normal; ">=</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">new</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;CCodec();</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; pCodec</span></span><span style="line-height: normal; ">-&gt;</span><span style="line-height: normal; ">Decode(NULL,</span><span style="line-height: normal; ">0</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">,NULL,NULL);</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; pCodec</span></span><span style="line-height: normal; ">-&gt;</span><span style="line-height: normal; ">Encode(NULL,</span><span style="line-height: normal; ">0</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">,NULL,NULL);</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp; delete (CCodec</span></span><span style="line-height: normal; ">*</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">)pCodec;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;</span></span><span style="line-height: normal; ">return</span><span style="line-height: normal; ">&nbsp;</span><span style="line-height: normal; ">0</span><span style="line-height: normal; "><span style="line-height: normal; font-size: 14px; color: #000000; ">;</span><br style="line-height: normal; " /><span style="line-height: normal; font-size: 14px; color: #000000; ">}</span><br style="line-height: normal; " /></span></div><p style="line-height: normal; "><span style="line-height: normal; font-size: 14px; ">&nbsp;</span></p><span style="line-height: normal; font-size: 14px; ">上面的ICodec接口等价于下面的定义：</span><span style="line-height: normal; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; "><span style="line-height: normal; ">class ICodec</span><br style="line-height: normal; " /><span style="line-height: normal; ">{</span><br style="line-height: normal; " /><span style="line-height: normal; ">public:</span><br style="line-height: normal; " /><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;virtual bool Decode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)=0;</span><br style="line-height: normal; " /><span style="line-height: normal; ">&nbsp;&nbsp;&nbsp;&nbsp;virtual bool Encode(char * lpDataSrc,unsigned int nSrcLen,char * lpDataDst,unsigned int *pnDstLen)=0;</span><br style="line-height: normal; " /><span style="line-height: normal; ">};</span></div></span></span><img src ="http://www.cppblog.com/jokes000/aggbug/158083.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-11 21:57 <a href="http://www.cppblog.com/jokes000/archive/2011/10/11/158083.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++类继承的问题</title><link>http://www.cppblog.com/jokes000/archive/2011/10/09/157899.html</link><dc:creator>Voices.</dc:creator><author>Voices.</author><pubDate>Sun, 09 Oct 2011 13:51:00 GMT</pubDate><guid>http://www.cppblog.com/jokes000/archive/2011/10/09/157899.html</guid><wfw:comment>http://www.cppblog.com/jokes000/comments/157899.html</wfw:comment><comments>http://www.cppblog.com/jokes000/archive/2011/10/09/157899.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/jokes000/comments/commentRss/157899.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/jokes000/services/trackbacks/157899.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;公用继承(public)：基类成员保持自己的访问级别。<br />&nbsp; &nbsp; &nbsp; 受保护继承(protected)：基类的public和protected成员在派生类中为protected成员。<br />&nbsp; &nbsp; &nbsp; 私有继承(private)：积累的所有成员在派生类中为private成员。 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 示例见《C++ Primer》 P483<br /><br />&nbsp; &nbsp; &nbsp; 默认继承保护级别：<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class Base { /* ... */ };<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; struct D1 : Base { /* ... */ }; &nbsp; // <span style="color: red; ">public</span> inheritance by default<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class D2 : Base { /* ... */ }; &nbsp; &nbsp;// <span style="color: red; ">private</span> inheritance by default<img src ="http://www.cppblog.com/jokes000/aggbug/157899.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/jokes000/" target="_blank">Voices.</a> 2011-10-09 21:51 <a href="http://www.cppblog.com/jokes000/archive/2011/10/09/157899.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>