﻿<?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++博客-千里之行，始于足下</title><link>http://www.cppblog.com/rednight/</link><description /><language>zh-cn</language><lastBuildDate>Tue, 14 Apr 2026 23:06:41 GMT</lastBuildDate><pubDate>Tue, 14 Apr 2026 23:06:41 GMT</pubDate><ttl>60</ttl><item><title>《effective c++ II》学习笔记 Item 2  Prefer iostream to stdio.h</title><link>http://www.cppblog.com/rednight/archive/2007/12/10/38178.html</link><dc:creator>rednight</dc:creator><author>rednight</author><pubDate>Mon, 10 Dec 2007 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/rednight/archive/2007/12/10/38178.html</guid><wfw:comment>http://www.cppblog.com/rednight/comments/38178.html</wfw:comment><comments>http://www.cppblog.com/rednight/archive/2007/12/10/38178.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rednight/comments/commentRss/38178.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rednight/services/trackbacks/38178.html</trackback:ping><description><![CDATA[<span style="font-family: Verdana;">关键词：safety, extensibility</span><br style="font-family: Verdana;"><br style="font-family: Verdana;"><span style="font-family: Verdana;">
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;i;<br>Rational&nbsp;r;&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;r&nbsp;is&nbsp;a&nbsp;rational&nbsp;number</span><span style="color: #008000;"><br></span><span style="color: #000000;"><img src="http://www.cppblog.com/Images/dot.gif"><br>cin&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;r;<br>cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;r;<br><br><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;Rational&nbsp;{<br></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br>&nbsp;&nbsp;Rational(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;numerator&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">0</span><span style="color: #000000;">,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;denominator&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br>&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"><br></span><span style="color: #0000ff;">private</span><span style="color: #000000;">:<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;n,&nbsp;d;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;numerator&nbsp;and&nbsp;denominator</span><span style="color: #008000;"><br></span><span style="color: #000000;">friend&nbsp;ostream</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">operator</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">(ostream</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;s,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;Rational</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;r);<br>};<br>ostream</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">operator</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">(ostream</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;s,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;Rational</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;r)<br>{<br>&nbsp;&nbsp;s&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;r.n&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">'</span><span style="color: #000000;">/</span><span style="color: #000000;">'</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;r.d;<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;s;<br>}<br></span></div>
</span><br style="font-family: Verdana;"><span style="font-family: Verdana;">绕开scanf(),printf()恼人的格式要求。使用便捷，安全的&lt;&lt;,&gt;&gt;操作符。</span><br style="font-family: Verdana;"><br style="font-family: Verdana;"><span style="font-family: Verdana;">最后，</span><br style="font-family: Verdana;"><span style="font-family: Verdana;">#include&lt;iostream.h&gt;是全局意义上的库函数声明，容易冲突。</span><br style="font-family: Verdana;"><span style="font-family: Verdana;">#include&lt;iostream&gt;是STL的写法，要名字空间std::声明。</span><br style="font-family: Verdana;"><br style="font-family: Verdana;">
<br> <img src ="http://www.cppblog.com/rednight/aggbug/38178.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rednight/" target="_blank">rednight</a> 2007-12-10 16:12 <a href="http://www.cppblog.com/rednight/archive/2007/12/10/38178.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《effective c++ II》学习笔记 Item1 Prefer const and inline to #define</title><link>http://www.cppblog.com/rednight/archive/2007/12/10/38167.html</link><dc:creator>rednight</dc:creator><author>rednight</author><pubDate>Mon, 10 Dec 2007 06:36:00 GMT</pubDate><guid>http://www.cppblog.com/rednight/archive/2007/12/10/38167.html</guid><wfw:comment>http://www.cppblog.com/rednight/comments/38167.html</wfw:comment><comments>http://www.cppblog.com/rednight/archive/2007/12/10/38167.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rednight/comments/commentRss/38167.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rednight/services/trackbacks/38167.html</trackback:ping><description><![CDATA[宏定义<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;ASPECT_RATIO&nbsp;1.653</span></div>
<br>主要是宏在预编译的时候被移除，不加入编译器编译，不利于错误的检测，给调试维护带来一定困难。<br>因而用<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;ASPECT_RATIO&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1.653</span><span style="color: #000000;">;</span></div>
<br>代替。<br>存在2个小问题需要注意:<br>1)&nbsp;&nbsp; &nbsp;指针常量问题<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;char&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;authorName&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Scott&nbsp;Meyers</span><span style="color: #000000;">"</span><span style="color: #000000;">;</span></div>
<br>需要2次使用&#8221;const&#8221;<br>2)&nbsp;&nbsp; &nbsp;在类的定义中<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;GamePlayer&nbsp;{<br></span><span style="color: #0000ff;">private</span><span style="color: #000000;">:<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;NUM_TURNS&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;constant&nbsp;declaration</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;scores[NUM_TURNS];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;use&nbsp;of&nbsp;constant</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"><br>};</span></div>
<br>此外，还必须在.cpp文件中予以定义:<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;GamePlayer::NUM_TURNS;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;mandatory&nbsp;definition;<br>&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;goes&nbsp;in&nbsp;class&nbsp;impl.&nbsp;file</span></div>
<br><br>值得注意的是老的编译器不支持这种表达方式，因此要采用如下的方式：<br>&nbsp;<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;EngineeringConstants&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;this&nbsp;goes&nbsp;in&nbsp;the&nbsp;class</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">private</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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;header&nbsp;file</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">static</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;FUDGE_FACTOR;<br>&nbsp;&nbsp;<img src="http://www.cppblog.com/Images/dot.gif"><br>};<br><br></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;this&nbsp;goes&nbsp;in&nbsp;the&nbsp;class&nbsp;implementation&nbsp;file</span><span style="color: #008000;"><br></span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">double</span><span style="color: #000000;">&nbsp;EngineeringConstants::FUDGE_FACTOR&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1.35</span><span style="color: #000000;">;</span></div>
<br><br>这种情形下如果要在类中定义常量数组，需要采用枚举类型做一折中处理：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<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;GamePlayer&nbsp;{<br></span><span style="color: #0000ff;">private</span><span style="color: #000000;">:<br>&nbsp;&nbsp;</span><span style="color: #0000ff;">enum</span><span style="color: #000000;">&nbsp;{&nbsp;NUM_TURNS&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">5</span><span style="color: #000000;">&nbsp;};&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;"the&nbsp;enum&nbsp;hack"&nbsp;—&nbsp;makes<br>&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;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;NUM_TURNS&nbsp;a&nbsp;symbolic&nbsp;name<br>&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;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;for&nbsp;5</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;scores[NUM_TURNS];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;fine</span><span style="color: #008000;"><br></span><span style="color: #000000;"><img src="http://www.cppblog.com/Images/dot.gif"><br>};</span></div>
<br><br>避免<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;max(a,b)&nbsp;((a)&nbsp;&gt;&nbsp;(b)&nbsp;?&nbsp;(a)&nbsp;:&nbsp;(b))</span></div>
<br>这种写法。<br>采用内连函数：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">inline&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;max(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;a,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;b)&nbsp;{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;a&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;b&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;a&nbsp;:&nbsp;b;&nbsp;}</span></div>
<br>增强适应性，应采用模板类：<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">template</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br>inline&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;max(</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;a,&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">&nbsp;b)<br>{&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;a&nbsp;</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;b&nbsp;</span><span style="color: #000000;">?</span><span style="color: #000000;">&nbsp;a&nbsp;:&nbsp;b;&nbsp;}</span></div>
<br>总结：并不是说不使用宏，要明确地知道使用宏后可能会引起的后果，减少有歧义的情况发生。<br>&nbsp;<br> <img src ="http://www.cppblog.com/rednight/aggbug/38167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rednight/" target="_blank">rednight</a> 2007-12-10 14:36 <a href="http://www.cppblog.com/rednight/archive/2007/12/10/38167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>《effective c++ II》Introduction</title><link>http://www.cppblog.com/rednight/archive/2007/12/06/37914.html</link><dc:creator>rednight</dc:creator><author>rednight</author><pubDate>Thu, 06 Dec 2007 09:08:00 GMT</pubDate><guid>http://www.cppblog.com/rednight/archive/2007/12/06/37914.html</guid><wfw:comment>http://www.cppblog.com/rednight/comments/37914.html</wfw:comment><comments>http://www.cppblog.com/rednight/archive/2007/12/06/37914.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/rednight/comments/commentRss/37914.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/rednight/services/trackbacks/37914.html</trackback:ping><description><![CDATA[内容见<a href="http://www.cppblog.com/rednight/articles/37913.html">http://www.cppblog.com/rednight/articles/37913.html</a><br>为啥只有随笔能显示在主页面上呢？
<img src ="http://www.cppblog.com/rednight/aggbug/37914.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/rednight/" target="_blank">rednight</a> 2007-12-06 17:08 <a href="http://www.cppblog.com/rednight/archive/2007/12/06/37914.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>