﻿<?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++博客-love in C++, live on MFC-随笔分类-C/C++</title><link>http://www.cppblog.com/flyingxu/category/823.html</link><description>to get ready...</description><language>zh-cn</language><lastBuildDate>Thu, 30 Apr 2009 03:19:20 GMT</lastBuildDate><pubDate>Thu, 30 Apr 2009 03:19:20 GMT</pubDate><ttl>60</ttl><item><title>thinking in c++ 习题答案</title><link>http://www.cppblog.com/flyingxu/archive/2006/08/09/11022.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Tue, 08 Aug 2006 16:19:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/08/09/11022.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/11022.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/08/09/11022.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/11022.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/11022.html</trackback:ping><description><![CDATA[<p>这个答案本来是作者卖钱的，我忘了是从那个网站下载的了，估计不全。<br>现在有人要，就放上来吧</p>
<p><a href="http://www.cppblog.com/Files/flyingxu/ThinkinginCppExercise.rar">http://www.cppblog.com/Files/flyingxu/ThinkinginCppExercise.rar</a> <br><br>(未验证，可能不全，或者根本就不是答案)<br><br></p>
<img src ="http://www.cppblog.com/flyingxu/aggbug/11022.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-08-09 00:19 <a href="http://www.cppblog.com/flyingxu/archive/2006/08/09/11022.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Is MSDN wrong?  or I made a mistake? about static member function</title><link>http://www.cppblog.com/flyingxu/archive/2006/07/11/9697.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Tue, 11 Jul 2006 11:14:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/07/11/9697.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/9697.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/07/11/9697.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/9697.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/9697.html</trackback:ping><description><![CDATA[我在ms的group上问了个问题<a href="http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.vc.mfc&amp;tid=402a414c-a125-45bd-9cf4-c1819db7e3e4">http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.vc.mfc&amp;tid=402a414c-a125-45bd-9cf4-c1819db7e3e4</a>：<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"><font face="Tahoma"><span style="COLOR: #000000">in http://msdn2.microsoft.com/en-us/library/480b7kf7.aspx</span><span style="COLOR: #000000">,</span></font><font face="Tahoma"><span style="COLOR: #000000"> it says that <br />pGdiObject-&gt;DeleteTempMap()</span><span style="COLOR: #008000">;</span><span style="COLOR: #008000">  // Causes compiler error</span></font><span style="COLOR: #008000"><br /></span><span style="COLOR: #000000"><font face="Tahoma">But I'm confused for a static member can be called by the object or a pointer.<br />Is there anything wrong of my understanding?</font><br /></span></div>回答的人到蛮多，讨论的也很热烈，但都没有直接回答我的问题，我的问题是，对于静态函数，<br />pGdiObject-&gt;DeleteTempMap()<span style="COLOR: #008000">;</span><span style="COLOR: #008000">  // Causes compiler error，<br />到底对不对？如果不对，就是msdn错了，不过总觉得比较奇怪，msdn不该出这样的错的</span><img src ="http://www.cppblog.com/flyingxu/aggbug/9697.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-07-11 19:14 <a href="http://www.cppblog.com/flyingxu/archive/2006/07/11/9697.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>_asm int 3 的一个应用：在程序运行后再进入调试器</title><link>http://www.cppblog.com/flyingxu/archive/2006/07/06/9493.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Thu, 06 Jul 2006 09:35:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/07/06/9493.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/9493.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/07/06/9493.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/9493.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/9493.html</trackback:ping><description><![CDATA[调试程序的时候，一般<br />1）启动调试器，比如VC<br />2）在VC中设断点<br />3）操作程序，使得程序运行到断点，停下来<br />但是现在我遇到一个情况，希望调试的情景是这样的：双击某个文件，我的程序启动后，这个时候我想程序停下来，让我在VC中调试它。<br />但是，双击文件后启动的程序，是个独立的exe运行程序，肯定不是在VC的运行控制下的，VC中设置的断点不起作用的。怎么办呢？<br />这个时候可以在想让程序停下来的地方增加指令_asm int 3，debug下编译好。双击文件后运行这个exe，在exe运行到_asm int 3时，就会中断，而把当前的exe运行与一个新的调试器（一个新的VC进程）结合起来。接下来就像一般的调试了。<br />这时的调试步骤类似与：<br />1）程序运行<br />2）遇到特定条件时，_asm int 3把当前exe与一个新的调试器，比如一个新的VC进程，联系起来<br />3）在调试器中调试exe<br /><br />：）<img src ="http://www.cppblog.com/flyingxu/aggbug/9493.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-07-06 17:35 <a href="http://www.cppblog.com/flyingxu/archive/2006/07/06/9493.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Make header files self-sufficient的一个例子</title><link>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Fri, 23 Jun 2006 12:56:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/8908.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/8908.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/8908.html</trackback:ping><description><![CDATA[C＋＋编程规范中第23条中说：<br />If one header file won't work unless the file that includes it also includes another header, that's gauche and puts unnecessary burden on that header file's users.<br />呵呵，英语有点拗口，后面举的例子也都是模板的例子。我倒是遇到过一个实际的例子，简化后如下：<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"><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">a.h</span></font><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff"><font face="Courier New">class</font></span><font face="Courier New"><span style="COLOR: #000000"> A <br />{<br /> </span><span style="COLOR: #0000ff">int</span></font><span style="COLOR: #000000"><font face="Courier New"> a;<br />};</font></span></div> <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"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">b.h<br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">注意：b.h并没有include a.h</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff"><font face="Courier New">class</font></span><span style="COLOR: #000000"><font face="Courier New"> B <br />{<br /> A a;<br />};</font> </span></div>用的时候怎么用呢？比如在main.cpp中想用class B<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"><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">main.cpp</span></font><span style="COLOR: #008000"><br /></span><font face="Courier New"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a.h</span><span style="COLOR: #000000">"</span></font><span style="COLOR: #000000"><br /><font face="Courier New">#include </font></span><font face="Courier New"><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b.h</span><span style="COLOR: #000000">"</span></font><span style="COLOR: #000000"><br /></span><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /></span></font><span style="COLOR: #008000"><br /></span><font face="Courier New"><span style="COLOR: #000000">B b;<br /></span><span style="COLOR: #008000">//</span></font><font face="Courier New"><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">do something on b</span></font></div>可以看到，main为了使用B，还得包含a.h，这就是上面一段英语说的：<br />如果一个头文件（b.h）不能工作，除非包含它（b.h）的文件（也就是main.cpp）也包含另一个头文件（a.h）...<br /><br />hehe，当我第一次遇到这种情况时，想了半天也想不明白为什么我明明包含了b.h还是不能用B啊<br /><br />那么，正确的写法应该是什么呢？<br />在类B的作者在设计B的时候，就应该想到用户只需要#include "b.h"就可以使用这个class B。所以，在b.h文件中，应该写明#include "a.h"，而不是让用户在main.cpp中去include "a.h"<br /><br />:)<img src ="http://www.cppblog.com/flyingxu/aggbug/8908.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-06-23 20:56 <a href="http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[zz]最全的printf参数意义表</title><link>http://www.cppblog.com/flyingxu/archive/2006/06/16/8635.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Fri, 16 Jun 2006 06:05:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/06/16/8635.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/8635.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/06/16/8635.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/8635.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/8635.html</trackback:ping><description><![CDATA[在<a class="singleposttitle" id="viewpost1_TitleUrl" href="http://blog.vckbase.com/flyingleaf/archive/2004/12/27/2335.html">关于printf的大部分参数祥解</a> (<a href="http://blog.vckbase.com/flyingleaf/archive/2004/12/27/2335.aspx">http://blog.vckbase.com/flyingleaf/archive/2004/12/27/2335.aspx</a>)文章后面的<strong><font style="BACKGROUND-COLOR: #ccffff">评论</font></strong>中,看到了到目前为止我看到过的最全的printf参数意义表.<br />以前一直在msdn里查,都没有这么详细的.<br />其实它是gnu c的一个文档,<font color="#000000">Formatted </font><span id="highlight_tag" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; FONT-WEIGHT: bold; PADDING-BOTTOM: 0px; COLOR: #ee6600; PADDING-TOP: 0px; BACKGROUND-COLOR: #ffff00; EE6600: "><font style="BACKGROUND-COLOR: #ffffff"><font color="#000000">Output</font> (</font><a href="http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted-Output"><font style="BACKGROUND-COLOR: #ffffff">http://www.gnu.org/software/libc/manual/html_node/Formatted-Output.html#Formatted-Output</font></a><font style="BACKGROUND-COLOR: #ffffff">)</font></span><img src ="http://www.cppblog.com/flyingxu/aggbug/8635.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-06-16 14:05 <a href="http://www.cppblog.com/flyingxu/archive/2006/06/16/8635.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>尽可能的使用全局变量errno来判断状态</title><link>http://www.cppblog.com/flyingxu/archive/2006/04/13/5480.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Thu, 13 Apr 2006 11:15:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/04/13/5480.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/5480.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/04/13/5480.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/5480.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/5480.html</trackback:ping><description><![CDATA[如果看到这个标题时,还不知道什么是errno,那么,你就和我一样.呵呵,我编程也有4,5年了,今天才知道errno.<br />errno不是我定义的一个变量,也不是否个随意的变量名,而是crt库中定义的一个全局变量<br />定义:errno Constants (<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_errno_Constants.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_errno_Constants.asp</a>)<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"><font face="Courier New"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /><span style="COLOR: #0000ff">#if</span><span style="COLOR: #000000">     (defined(_MT) || defined(_DLL)) &amp;&amp; !defined(_MAC)</span></font><span style="COLOR: #000000"><br /><font face="Courier New"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />_CRTIMP </font></span><font face="Courier New"><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> __cdecl _errno(</span><span style="COLOR: #0000ff">void</span></font><font face="Courier New"><span style="COLOR: #000000">);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000"> errno   (*_errno())</span></font><span style="COLOR: #000000"><br /><font face="Courier New"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></font></span><font face="Courier New"><span style="COLOR: #0000ff">#else</span><span style="COLOR: #000000">   /* ndef _MT &amp;&amp; ndef _DLL */</span></font><span style="COLOR: #000000"><br /><font face="Courier New"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />_CRTIMP </font></span><font face="Courier New"><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">int</span></font><font face="Courier New"><span style="COLOR: #000000"> errno;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000">  /* _MT || _DLL */</span></font><span style="COLOR: #000000"><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /></span></div>实际上,就是再一些crt函数调用后,errno会被赋值,表示函数调用的状态.有点类似window api中的GetLastError<br />这些crt函数包括fgetpos或者ftell和strtol之类.<br />在什么时候用它呢?看下面的一个例子:<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"><font face="Courier New"><span style="COLOR: #000000">    const </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> chTest </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">123456789222299999999</span><span style="COLOR: #000000">"</span></font><font face="Courier New"><span style="COLOR: #000000">;<br />    </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000"> pStop </span><span style="COLOR: #000000">=</span></font><font face="Courier New"><span style="COLOR: #000000"> NULL;<br />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> nValue </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> strtol(chTest, </span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">pStop, </span><span style="COLOR: #000000">10</span></font><font face="Courier New"><span style="COLOR: #000000">);<br /><br />    </span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> nError </span><span style="COLOR: #000000">=</span></font><font face="Courier New"><span style="COLOR: #000000"> errno;<br />    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(nError </span><span style="COLOR: #000000">==</span></font><span style="COLOR: #000000"><font face="Courier New"> ERANGE)<br />        perror(chTest);</font></span></div>这个时候,nValue = 0x7fffffff,你如果只是根据它来判断,是不知道实际上已经出错了.所以,这个时候必须借助errno来判断状态.<br /><br />惭愧啊,今天才知道.今天看strtol的msdn文档,看到一句话For both functions, <b>errno</b> is set to <b>ERANGE</b> if overflow or underflow occurs.,我看了半天没有找到errno,后来一搜,才发现它居然是个全局变量.<img src ="http://www.cppblog.com/flyingxu/aggbug/5480.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-04-13 19:15 <a href="http://www.cppblog.com/flyingxu/archive/2006/04/13/5480.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个cpp网站</title><link>http://www.cppblog.com/flyingxu/archive/2006/03/07/3872.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Tue, 07 Mar 2006 13:45:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/03/07/3872.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/3872.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/03/07/3872.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/3872.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/3872.html</trackback:ping><description><![CDATA[<A href="http://www.cpper.com/">http://www.cpper.com/</A><BR>那里论坛里的都是大牛，说的话我听不懂，插不上嘴。<img src ="http://www.cppblog.com/flyingxu/aggbug/3872.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-03-07 21:45 <a href="http://www.cppblog.com/flyingxu/archive/2006/03/07/3872.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>什么时候必须显式调用析构函数？</title><link>http://www.cppblog.com/flyingxu/archive/2006/03/04/3702.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Fri, 03 Mar 2006 16:35:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/03/04/3702.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/3702.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/03/04/3702.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/3702.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/3702.html</trackback:ping><description><![CDATA[在《<A class=singleposttitle id=viewpost1_TitleUrl href="/apollo/archive/2006/03/03/CPPNOTES.html"><FONT color=#333333>工作中发现的</FONT></A> 》中，提到析构函数可以自己调用，并用一个例子编译、运行证明了。<BR>现在有个问题，除了知道“析构函数<STRONG><FONT style="BACKGROUND-COLOR: #0000ff">可以</FONT></STRONG>自己调用”外，那么什么时候<STRONG><FONT style="BACKGROUND-COLOR: #ff0000">必须</FONT></STRONG>显式调用析构函数？<BR><BR>先看一段现实生活中的代码吧，mfc源码中：<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"><SPAN style="COLOR: #000000">BOOL&nbsp;CStatusBar::AllocElements(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;nElements,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;cbElement)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;destruct&nbsp;old&nbsp;elements</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;AFX_STATUSPANE</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pSBP&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;_GetPanePtr(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;m_nCount;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pSBP</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">strText.</SPAN><SPAN style="COLOR: #000000"><STRONG>~</STRONG></SPAN><SPAN style="COLOR: #000000"><STRONG>CString();&nbsp;&nbsp;&nbsp;//注意看这里</STRONG><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">pSBP;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;allocate&nbsp;new&nbsp;elements</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">if</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #000000">!</SPAN><SPAN style="COLOR: #000000">CControlBar::AllocElements(nElements,&nbsp;cbElement))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;FALSE;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;construct&nbsp;new&nbsp;elements</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;pSBP&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;_GetPanePtr(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;m_nCount;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">pSBP</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">strText,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">afxEmptyString,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(CString));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">pSBP;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<BR>}<BR></SPAN></DIV>在上面的代码中，就有显式调用CString的析构函数的代码。cool。<BR>因为还调用了<SPAN style="COLOR: #000000">CControlBar::AllocElements()，上面的代码不是很明显，我把<SPAN style="COLOR: #000000">CControlBar::AllocElements简化一下后：<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"><SPAN style="COLOR: #000000">BOOL&nbsp;CStatusBar::AllocElements(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;nElements,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;cbElement)<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;destruct&nbsp;old&nbsp;elements</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;AFX_STATUSPANE</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pSBP&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;_GetPanePtr(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;m_nCount;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pSBP</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">strText.</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN style="COLOR: #000000">CString();&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">注意看这里</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">pSBP;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;allocate&nbsp;new&nbsp;elements<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">if&nbsp;(!CControlBar::AllocElements(nElements,&nbsp;cbElement))<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;FALSE;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">简化后的代码，实际运行肯定有问题，但是关键东西出来了</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;free(pSBP);</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">注意这里调用的是free</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;pSBP&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;calloc(nElements,&nbsp;cbElement);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;construct&nbsp;new&nbsp;elements</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;pSBP&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;_GetPanePtr(</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">);&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">根据mfc的代码，可以理解这里的pSBP和前面的pSBP还是同一个地址</SPAN><SPAN style="COLOR: #008000"><BR></SPAN><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">for</SPAN><SPAN style="COLOR: #000000">&nbsp;(i&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">0</SPAN><SPAN style="COLOR: #000000">;&nbsp;i&nbsp;</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;m_nCount;&nbsp;i</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;memcpy(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">pSBP</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">strText,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">afxEmptyString,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(CString));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #000000">++</SPAN><SPAN style="COLOR: #000000">pSBP;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;TRUE;<BR>}</SPAN></DIV></SPAN></SPAN>这个时候，如果注意到我特别注释的free函数调用，可能已经意识到了为什么要显式调用析构函数了。<BR>如果还没有，那么可以问自己一个面试常规问题：delete和free有什么区别？答：delete会使析构函数被调用。<BR>或者反过来说，<FONT style="BACKGROUND-COLOR: #ff0000"><STRONG>free没有调用析构函数，那么怎么办？所以你必须自己显示调用析构函数</STRONG></FONT>。<IMG height=19 src="http://www.cppblog.com/Emoticons/regular_smile.gif" width=19 border=0><BR><BR>上面的这个例子可以这样抽象下，现在需要free掉一块内存，而那块内存中，还有一个类，类里面还有指针，（这里是CString）需要在析构函数中释放内存。因为用的是free，所以那个类的析构函数不会自动被调用，这个时候，就<STRONG>必须显式</STRONG>调用那个类的析构函数。<BR><BR>这个是不是很偏的问题呢？遇到了就看看，没有遇到过，也不会影响日常工作，哈。<BR><BR>另外继续问个面试问题，new和calloc的区别？哈，构造的函数的调用啊<BR>所以，上面的代码用的calloc，就必须显示调用构造函数啊，在哪里呢？就是<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"><SPAN style="COLOR: #000000">memcpy(</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">pSBP</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">strText,&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">afxEmptyString,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">sizeof</SPAN><SPAN style="COLOR: #000000">(CString));</SPAN></DIV>和CString的构造函数比较下：<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"><SPAN style="COLOR: #000000">_AFX_INLINE&nbsp;CString::CString()<BR>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;m_pchData&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;afxEmptyString.m_pchData;&nbsp;}</SPAN></DIV>但是，为什么不就直接调用构造函数呢？我也不知道。详见<A id=Comments1_CommentList__ctl0_NameLink target=_blank>dhong</A>下面的评论。（<A id=Comments1_CommentList__ctl0_NameLink target=_blank>dhong</A>纠正了我的一个错误）<BR><BR>不过，下面的代码<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"><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CString&nbsp;aStr;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;pStr&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN><SPAN style="COLOR: #000000">aStr&nbsp;;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pStr</SPAN><SPAN style="COLOR: #000000">-&gt;</SPAN><SPAN style="COLOR: #000000">CString();</SPAN></DIV>
<P>是编译不过的。</P>
<P><IMG height=19 src="http://www.cppblog.com/Emoticons/beer_mug.gif" width=19 border=0></P>
<P>&nbsp;</P><img src ="http://www.cppblog.com/flyingxu/aggbug/3702.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-03-04 00:35 <a href="http://www.cppblog.com/flyingxu/archive/2006/03/04/3702.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一种临时变量的命名形式</title><link>http://www.cppblog.com/flyingxu/archive/2006/03/03/3699.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Fri, 03 Mar 2006 15:14:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/03/03/3699.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/3699.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/03/03/3699.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/3699.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/3699.html</trackback:ping><description><![CDATA[今天看java，提到这样一种变量命名形式：<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"><SPAN style="COLOR: #000000">CBox&nbsp;aBox；</SPAN></DIV>也就是说，在命名一个没有太多实际意义的变量时，可以在前面加一个“a”。<BR>呵呵，我觉得这种命名方法还是蛮好的，可能是我看书看的少，在c++的书中还没有看到过这样的写法推荐。<BR>而在msdn中，常见的例子为：<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"><SPAN style="COLOR: #000000">CFileFind&nbsp;finder;</SPAN></DIV>或者<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"><SPAN style="COLOR: #000000">CFile&nbsp;myFile;</SPAN></DIV>呵呵，也是尽量写一个名词形式。<BR><BR>所以呢，新的写法可以写成<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"><SPAN style="COLOR: #000000">CFile&nbsp;aFile</SPAN></DIV><IMG height=19 src="http://www.cppblog.com/Emoticons/regular_smile.gif" width=19 border=0><BR><BR>细小的问题，不用钻牛角尖，好玩。<IMG height=19 src="http://www.cppblog.com/Emoticons/beer_mug.gif" width=19 border=0><img src ="http://www.cppblog.com/flyingxu/aggbug/3699.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-03-03 23:14 <a href="http://www.cppblog.com/flyingxu/archive/2006/03/03/3699.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于CString的两个问题</title><link>http://www.cppblog.com/flyingxu/archive/2006/03/02/3631.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Thu, 02 Mar 2006 01:29:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/03/02/3631.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/3631.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/03/02/3631.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/3631.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/3631.html</trackback:ping><description><![CDATA[<P><STRONG>关于CString的两个问题<BR></STRONG><FONT size=2>读</FONT><A class=postTitle2 id=viewpost1_TitleUrl href="http://dayouluo.cnblogs.com/archive/2005/04/17/139086.html"><FONT size=2>CString,string,char*的综合比较(一)</FONT></A>&nbsp;<BR><BR>1 CString的基类<BR>在《比较》一文中，写到：<BR>“CString（typedef CStringT&lt;TCHAR, StrTraitMFC&lt;TCHAR&gt;&gt; CString）为Visual C++中最常用的字符串类，继承自CSimpleStringT类，主要应用在MFC和ATL编程中，主要数据类型有char(应用于ANSI)，wchar_t(unicode)，TCHAR(ANSI与unicode均可)；”<BR>我第一感觉就是文章写错了，因为我看过CString的源码的啊：<BR></P>
<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 src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff"><STRONG>struct</STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;CStringData<BR><IMG id=Codehighlighter1_19_292_Open_Image onclick="this.style.display='none'; Codehighlighter1_19_292_Open_Text.style.display='none'; Codehighlighter1_19_292_Closed_Image.style.display='inline'; Codehighlighter1_19_292_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_19_292_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_19_292_Closed_Text.style.display='none'; Codehighlighter1_19_292_Open_Image.style.display='inline'; Codehighlighter1_19_292_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_19_292_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 src="http://www.cppblog.com/images/dot.gif"></SPAN></STRONG><SPAN id=Codehighlighter1_19_292_Open_Text><STRONG><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">long</SPAN><SPAN style="COLOR: #000000">&nbsp;nRefs;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;reference&nbsp;count</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;nDataLength;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;length&nbsp;of&nbsp;data&nbsp;(including&nbsp;terminator)</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;nAllocLength;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN></STRONG><STRONG><SPAN style="COLOR: #008000">&nbsp;length&nbsp;of&nbsp;allocation<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;TCHAR&nbsp;data[nAllocLength]</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><SPAN style="COLOR: #000000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;TCHAR</STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">&nbsp;data()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;TCHAR*&nbsp;to&nbsp;managed&nbsp;data</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG id=Codehighlighter1_263_290_Open_Image onclick="this.style.display='none'; Codehighlighter1_263_290_Open_Text.style.display='none'; Codehighlighter1_263_290_Closed_Image.style.display='inline'; Codehighlighter1_263_290_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_263_290_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_263_290_Closed_Text.style.display='none'; Codehighlighter1_263_290_Open_Image.style.display='inline'; Codehighlighter1_263_290_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN id=Codehighlighter1_263_290_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 src="http://www.cppblog.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_263_290_Open_Text><SPAN style="COLOR: #000000">{&nbsp;</SPAN><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;(TCHAR</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">)(</SPAN><SPAN style="COLOR: #0000ff">this</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000">1</SPAN><SPAN style="COLOR: #000000">);&nbsp;}</SPAN></SPAN></STRONG><SPAN style="COLOR: #000000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</STRONG></SPAN></SPAN><STRONG><SPAN style="COLOR: #000000">;<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">class</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">&nbsp;CString<BR><IMG id=Codehighlighter1_310_934_Open_Image onclick="this.style.display='none'; Codehighlighter1_310_934_Open_Text.style.display='none'; Codehighlighter1_310_934_Closed_Image.style.display='inline'; Codehighlighter1_310_934_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_310_934_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_310_934_Closed_Text.style.display='none'; Codehighlighter1_310_934_Open_Image.style.display='inline'; Codehighlighter1_310_934_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_310_934_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 src="http://www.cppblog.com/images/dot.gif"></SPAN></STRONG><SPAN id=Codehighlighter1_310_934_Open_Text><STRONG><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">:<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN></STRONG><STRONG><SPAN style="COLOR: #008000">&nbsp;Constructors<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;constructs&nbsp;empty&nbsp;CString</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString();<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;copy&nbsp;constructor</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;CString</SPAN><SPAN style="COLOR: #000000">&amp;</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">&nbsp;stringSrc);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;from&nbsp;a&nbsp;single&nbsp;character</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(TCHAR&nbsp;ch,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">&nbsp;nRepeat&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">1</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;from&nbsp;an&nbsp;ANSI&nbsp;string&nbsp;(converts&nbsp;to&nbsp;TCHAR)</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(LPCSTR&nbsp;lpsz);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;from&nbsp;a&nbsp;UNICODE&nbsp;string&nbsp;(converts&nbsp;to&nbsp;TCHAR)</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(LPCWSTR&nbsp;lpsz);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;subset&nbsp;of&nbsp;characters&nbsp;from&nbsp;an&nbsp;ANSI&nbsp;string&nbsp;(converts&nbsp;to&nbsp;TCHAR)</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(LPCSTR&nbsp;lpch,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">&nbsp;nLength);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;subset&nbsp;of&nbsp;characters&nbsp;from&nbsp;a&nbsp;UNICODE&nbsp;string&nbsp;(converts&nbsp;to&nbsp;TCHAR)</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(LPCWSTR&nbsp;lpch,&nbsp;</SPAN><SPAN style="COLOR: #0000ff">int</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">&nbsp;nLength);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;from&nbsp;unsigned&nbsp;characters</SPAN></STRONG><SPAN style="COLOR: #008000"><BR><STRONG><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></STRONG></SPAN><STRONG><SPAN style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;CString(</SPAN><SPAN style="COLOR: #0000ff">const</SPAN><SPAN style="COLOR: #000000">&nbsp;unsigned&nbsp;</SPAN><SPAN style="COLOR: #0000ff">char</SPAN><SPAN style="COLOR: #000000">*</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">&nbsp;psz);<BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">&nbsp;Attributes&nbsp;&amp;&nbsp;Operations</SPAN></STRONG></DIV>
<P></SPAN><STRONG>但是看文章的样子，也不像是粗制滥造的，心里很是疑惑。<BR>后来再打开VC7的源码看，果然：<BR></STRONG></P>
<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"><STRONG><SPAN style="COLOR: #000000">typedef&nbsp;ATL::CStringT</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;TCHAR,&nbsp;StrTraitMFC</SPAN><SPAN style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">&nbsp;TCHAR&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">&gt;</SPAN></STRONG><SPAN style="COLOR: #000000"><STRONG>&nbsp;CString;<BR></STRONG></SPAN></DIV>
<P><STRONG>看来微软在不知不觉中做了这么大的变化。<BR><BR>2 <BR>“string是方便的，可以从几乎所有的字符串构造而来，包括CString和char*；&nbsp;<BR>&nbsp;CString次之，可以从基本的一些字符串变量构造而来，包括char*等； ”<BR>的确，如下的代码是可以编译的：<BR></STRONG></P>
<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"><STRONG><SPAN style="COLOR: #000000">CString&nbsp;strTest&nbsp;</SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">test</SPAN><SPAN style="COLOR: #000000">"</SPAN></STRONG><STRONG><SPAN style="COLOR: #000000">;<BR></SPAN><SPAN style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">&nbsp;sTest(strTest);</SPAN></STRONG></DIV>
<P><STRONG>但是这是表明string比CString方便吗？<BR>实际上，在string sTest(strTest);中有一个函数被调用，就是：<BR></STRONG></P>
<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"><STRONG><SPAN style="COLOR: #000000">_AFX_INLINE&nbsp;CString::</SPAN><SPAN style="COLOR: #0000ff">operator</SPAN><SPAN style="COLOR: #000000">&nbsp;LPCTSTR()&nbsp;</SPAN><SPAN style="COLOR: #0000ff">const</SPAN></STRONG><SPAN style="COLOR: #000000"><BR><STRONG>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</STRONG></SPAN><STRONG><SPAN style="COLOR: #0000ff">return</SPAN><SPAN style="COLOR: #000000">&nbsp;m_pchData;&nbsp;}</SPAN></STRONG></DIV>
<P><STRONG>然后才是string构造生成，这能说是string比CString方便吗？应该说是CString比string方便才好。<BR>虽然可以用<BR></STRONG></P>
<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"><SPAN style="COLOR: #000000"><STRONG>CString&nbsp;strTest(sTest.c_str()&nbsp;);</STRONG></SPAN></DIV>
<P><STRONG>来初始化CString，但是CString没有参数为string的构造函数，也是一个小小的遗憾，毕竟string是c++标准库的一部分了。</STRONG></P><img src ="http://www.cppblog.com/flyingxu/aggbug/3631.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-03-02 09:29 <a href="http://www.cppblog.com/flyingxu/archive/2006/03/02/3631.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>