﻿<?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/keyws/category/2482.html</link><description>C++ &amp;&amp; keyWordSpotting</description><language>zh-cn</language><lastBuildDate>Fri, 23 May 2008 19:43:44 GMT</lastBuildDate><pubDate>Fri, 23 May 2008 19:43:44 GMT</pubDate><ttl>60</ttl><item><title>【note】Effective C++ (2) - Shifting from C to C++</title><link>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Tue, 22 Aug 2006 09:24:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11580.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/22/11580.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11580.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11580.html</trackback:ping><description><![CDATA[
		<h1 style="MARGIN: 17pt 0cm 16.5pt">
				<span lang="EN-US">Shifting from C to C++</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">1. To C++ programmer, for example, a pointer to a pointer looks a little funny. Why, we wonder, wasn’t a reference to a pointer used <span style="mso-spacerun: yes"> </span>instead?</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char chr[] = "chenzhenshi&amp;guohonghua";</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char*<span style="mso-spacerun: yes">  </span>pchr = chr;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char** ppchr = &amp;pchr; </span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char*&amp;<span style="mso-spacerun: yes">  </span>rpchr<span style="mso-spacerun: yes">  </span>= pchr; // a reference to a pointer</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pchr &lt;&lt; ' ' &lt;&lt; *ppchr &lt;&lt; ' ' &lt;&lt; rpchr &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">2. C is a fairly simple language. All it really offers is <span style="COLOR: green">macros, pointers, structs, arrays, and functions</span>. No matter what the problem is, the solution will always boil down to macros, pointers, structs, arrays, and functions. Not so in C++. The macros, pointers, structs, arrays and functions are still there, of course, but so are <span style="COLOR: green">private and protected members, function overloading, default parameters, constructors and destructors, user-defined operators, inline functions, references, friends, templates, exceptions, namespaces, and more</span>. The design space is much richer in C++ than it is in C: there are just a lot more options to consider.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 1: Prefer const and inline to #define</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">3. The Item might better be called “prefer the compiler to the preprocessor”.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">4. <span style="mso-tab-count: 1">   </span>const char* pc;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>pc = a1;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>pc = a2;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>const char* const pcc = "a const pointer to a const char array";</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pcc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>// error C2166: l-value specifies const object</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>// pcc = a1;<span style="mso-spacerun: yes">  </span>// error!</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>std::cout &lt;&lt; pcc &lt;&lt; std::endl;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">5. You can define a const variable in a class, but it must be static const, and have a definition in an implementation file. </span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">// .h file</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">class try_const</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">{</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">public:</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<span style="mso-tab-count: 1">       </span>static const int num;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">};</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">// .cxx file</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">const int try_const::num = 250;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">6. You can get all the efficiency of a macro plus all the predictable behavior and type safety of a regular function by using an inline function.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Template &lt;class type&gt;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Inline const type&amp; max (const type&amp; a, const type&amp; b)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">{</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">Return a &gt; b ? a : b ;</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">}</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">7. Given the availability of consts and inlines, your need for the preprocessor is reduced, but it's not completely eliminated. The day is far from near when you can abandon #include, and #ifdef/#ifndef continue to play important roles in controlling compilation. It's not yet time to retire the preprocessor, but you should definitely plan to start giving it longer and more frequent vacations.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 2: Prefer &lt;iostream&gt; to &lt;stdio.h&gt;</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">8. <span style="mso-spacerun: yes"> </span>scanf and printf are not type-safe and extensible.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">9.<span style="mso-spacerun: yes">  </span>In particular, if you #include &lt;iostream&gt;, you get the elements of the iostream library ensconced within the namespace std (see Item 28), but if you #include &lt;iostream.h&gt;, you get those same elements at global scope. Getting them at global scope can lead to name conflicts, precisely the kinds of name conflicts the use of namespaces is designed to prevent.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 3: Prefer new and delete to malloc and free</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">10. The problem with malloc and free(and their variants) is simple : they don’t know about constructors and destructors.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">11. free</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">操作不会调用析构函数，如果指针所指对象本身又分配了内存，则会造成内存丢失。</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 4: Prefer C++ style comments</font>
				</span>
		</h2>
		<h1 style="MARGIN: 17pt 0cm 16.5pt">
				<span lang="EN-US">Memory Management</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">12. Memory management concerns in C++ fall into two general camps: getting it right and making it perform efficiently.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 5: Use the same form in corresponding uses of new and delete</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">13. When you use new, two things happen.</span>
				<span lang="EN-US" style="mso-font-kerning: 0pt">
				</span>
				<span lang="EN-US">First, memory is allocated. Second, one or more constructors are called for that memory. When you use delete, two other things happen: one or more destructors are called for the memory, then the memory is deallocated.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">14. The standard C++ library includes string and vector templates that reduce the need for built-in arrays to nearly zero.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 6: Use delete on pointer members in destructors</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">15. Speaking of smart pointers, one way to avoid the need to delete pointer members is to replace those members with smart pointer objects like the standard C++ Library’s auto_ptr.</span>
		</p>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 7: Be prepared for out-of-memory conditions</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 8: Adhere to convention when writing operator new and operator delete</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 9: Avoid hiding the “normal” form of new</font>
				</span>
		</h2>
		<h2 style="MARGIN: 13pt 0cm">
				<span lang="EN-US">
						<font face="Arial">Item 10: Write operator delete if you write operator new</font>
				</span>
		</h2>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">让我们回过头去看看这样一个基本问题：为什么有必要写自己的</span>
				<span lang="EN-US">operator new</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US">operator delete</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">？答案通常是：为了效率。缺省的</span>
				<span lang="EN-US">operator new</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US">operator delete</span>
				<span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具有非常好的通用性，它的这种灵活性也使得在某些特定的场合下，可以进一步改善它的性能。尤其在那些需要动态分配大量的但很小的对象的应用程序里，情况更是如此。</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<o:p> </o:p>
				</span>
		</p>
<img src ="http://www.cppblog.com/keyws/aggbug/11580.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-22 17:24 <a href="http://www.cppblog.com/keyws/archive/2006/08/22/11580.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【note】《C++ Primer － 8.4节“动态分配的对象”》学习笔记 </title><link>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Tue, 22 Aug 2006 06:54:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11573.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/22/11573.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11573.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11573.html</trackback:ping><description><![CDATA[
		<div class="postTitle" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span> <br /><span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">   动态分配的对象：程序员完全控制分配与释放，分配在程序的空闲存储区（<span lang="EN-US">free store)</span>的可用内存池中。<span lang="EN-US"><br /> <br /> 1</span>）单个对象的动态分配与释放；<span lang="EN-US"><br /> new</span>表达式没有返回实际分配的对象，而是返回指向该对象的指针。对该对象的全部操作都要通过这个指针间接完成。<span lang="EN-US"><br /> </span>随机分配的内存具有随机的位模式，建议初始化，例如：<span lang="EN-US"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?><o:p></o:p></span></span></div>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> int* pi = new int(0);</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<o:p>
						</o:p>
				</span>
		</p>
		<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
				<p class="MsoNormal" style="MARGIN: 0cm 0cm 12pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
						<br /> </p>
		</span>
		<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">空闲存储区是有限的资源，若被耗尽，<span lang="EN-US">new</span>表达式会失败，抛出<span lang="EN-US">bad_alloc</span>异常。<span lang="EN-US"><br /> </span>这样做没有必要<span lang="EN-US">:<o:p></o:p></span></span>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    </span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">if</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> ( pi != 0 )<br />        delete pi;</span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">说明：如果指针操作数被设置为<span lang="EN-US">0</span>，则<span lang="EN-US">C++</span>保证<span lang="EN-US">delete</span>表达式不会调用操作符<span lang="EN-US">delete()</span>。所以没有必要测试其是否为<span lang="EN-US">0</span>。<span lang="EN-US"><br /> </span>在<span lang="EN-US">delete</span>表达式之后，<span lang="EN-US">pi</span>被称作空悬指针，即指向无效内存的指针。空悬指针是程序错误的根源，建议对象释放后，将该指针设置为<span lang="EN-US">0</span>。<span lang="EN-US"><br /> <br /> 2</span>）<span lang="EN-US">auto_ptr<br /> auto_ptr</span>是<span lang="EN-US">C++</span>标准库提供的类模板，它可以帮助程序员自动管理用<span lang="EN-US">new</span>表达式动态分配的单个对象，但是，它没有对数组管理提供类似支持。它的头文件为：<span lang="EN-US"><o:p></o:p></span></span> </p>
		<div class="postText">
				<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: #000000">    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #000000">memory</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> </span>
				</div>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">当<span lang="EN-US">auto_ptr</span>对象的生命期结束时，动态分配的对象被自动释放。<span lang="EN-US"><br /> auto_ptr</span>类模板背后的主要动机是支持与普通指针类型相同的语法，但是为<span lang="EN-US">auto_ptr</span>对象所指对象的释放提供自动管理。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?>
						<v:shape id="_x0000_i1026" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">第一种初始化形式<span lang="EN-US"><br /><v:shape id="_x0000_i1027" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    std::auto_ptr&lt;</span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">&gt; pi( </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">new</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">(1024) );    </span>
						<font face="Verdana" color="#333333">
						</font>
				</font>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">// <v:shape id="_x0000_i1028" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> auto_ptr</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">类模板支持所有权概念，当一个<span lang="EN-US">auto_ptr</span>对象被用另一个<span lang="EN-US">auto_ptr</span>对象初始化赋值时，左边被赋值或初始化的对象就拥有了空闲存储区内底层对象的所有权，而右边的<span lang="EN-US">auto_ptr</span>对象则<font color="#ff0000">撤消所有责任</font>。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1029" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    std::auto_ptr&lt;std::</span>
						<span style="COLOR: blue">string</span>
						<span style="COLOR: black">&gt; pstr_auto( </span>
						<span style="COLOR: blue">new</span>
						<span style="COLOR: black"> std::</span>
						<span style="COLOR: blue">string</span>
						<span style="COLOR: black">( "Brontonsaurus" ) );<br /><v:shape id="_x0000_i1030" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape>    </span>
						<font color="#333333">
						</font>
						<span style="COLOR: green">// </span>
						<font color="#333333">
						</font>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">第二种初始化形式<span lang="EN-US"><br /><v:shape id="_x0000_i1031" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    std::auto_ptr&lt;std::</span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">string</span>
						<font face="Verdana" color="#333333">
						</font>
				</font>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">&gt; pstr_auto2( pstr_auto );<v:shape id="_x0000_i1032" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">判断是否指向一个对象，例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
				</span>
		</p>
		<div class="postText">
				<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">    </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 第三种初始化形式</span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #000000">    auto_ptr</span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000"> p_auto_int;    <br />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> ( p_auto_int.</span>
						<span style="COLOR: #0000ff">get</span>
						<span style="COLOR: #000000">() </span>
						<span style="COLOR: #000000">==</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">0</span>
						<span style="COLOR: #000000"> )<br />        <img src="http://www.cppblog.com/images/dot.gif" /><br />    </span>
						<span style="COLOR: #0000ff">else</span>
						<span style="COLOR: #000000">
								<br />        </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 重置底层指针，必须使用此函数        </span>
						<span style="COLOR: #008000">
								<br />
						</span>
						<span style="COLOR: #000000">        p_auto_int.reset( </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000">( </span>
						<span style="COLOR: #000000">1024</span>
						<span style="COLOR: #000000"> ) );<br /></span>
				</div>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<br /> 3<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">）数组的动态分配与释放<span lang="EN-US"><br /> </span>建议使用<span lang="EN-US">C++</span>标准库<span lang="EN-US">string,</span>避免使用<span lang="EN-US">C</span>风格字符串数组。<span lang="EN-US"><br /> </span>为避免动态分配数组的内存管理带来的问题，一般建议使用标准库<span lang="EN-US">vector</span>、<span lang="EN-US">list</span>或<span lang="EN-US">string</span>容器类型。<span lang="EN-US"><br /> <br /> 4</span>）常量对象的动态分配与释放<span lang="EN-US"><br /> </span>可以使用<span lang="EN-US">new</span>表达式在空闲存储区内创建一个<span lang="EN-US">const</span>对象，例：<span lang="EN-US"><o:p></o:p></span></span></p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1042" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">此时必须初始化，否则编译错误<span lang="EN-US"><br /><v:shape id="_x0000_i1043" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">    </span>
				<font size="2">
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">const</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">* pci = </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">new</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">const</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">int</span>
						<font face="Verdana" color="#333333">
						</font>
						<span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">(1024);    </span>
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"> </span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">我们不能在空闲存储区创建内置类型元素的<span lang="EN-US">const</span>数组，原因是：我们不能初始化用<span lang="EN-US">new</span>表达式创建的内置类型数组的元素。例：<span lang="EN-US"><o:p></o:p></span></span>
		</p>
		<p class="MsoNormal" style="BACKGROUND: #eeeeee; MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<v:shape id="_x0000_i1044" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt="">
								<v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif">
								</v:imagedata>
						</v:shape>
						<span style="COLOR: black">    </span>
						<span style="COLOR: blue">const</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">int</span>
						<span style="COLOR: black">* pci = </span>
						<span style="COLOR: blue">new</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">const</span>
						<span style="COLOR: black"> </span>
						<span style="COLOR: blue">int</span>
						<span style="COLOR: black">[100]; </span>
						<span style="COLOR: green">// </span>
				</span>
				<span style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">编译错误<span lang="EN-US"><v:shape id="_x0000_i1045" style="WIDTH: 8.25pt; HEIGHT: 12pt" type="#_x0000_t75" alt=""><v:imagedata src="file:///C:\DOCUME~1\bigpan\LOCALS~1\Temp\msohtml1\01\clip_image001.gif" o:href="/Images/OutliningIndicators/None.gif"></v:imagedata></v:shape></span></span>
				<font size="2">
						<font color="#333333">
								<font face="Verdana">
										<span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
												<o:p>
												</o:p>
										</span>
								</font>
						</font>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left; mso-pagination: widow-orphan" align="left">
				<span lang="EN-US" style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">
						<br /> 5</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体">）定位<span lang="EN-US">new</span>表达式<span lang="EN-US"><br /> new</span>表达式的第三种形式允许程序员要求将对象创建在已经被分配好的内存中。称为：定位<span lang="EN-US">new</span>表达式（<span lang="EN-US">placement new expression)</span>。程序员在<span lang="EN-US">new</span>表达式中指定待创建对象所在的内存地址。如下所示：<span lang="EN-US"><br /> new </span>（<span lang="EN-US">place_address) type-specifier<br /> </span>注意：<span lang="EN-US">place_address</span>必须是个指针，必须包含头文件<span lang="EN-US">&lt;new&gt;</span>。这项设施允许程序员预分配大量的内存，供以后通过这种形式的<span lang="EN-US">new</span>表达式创建对象。例如：<span lang="EN-US"><o:p></o:p></span></span>
				<br />
		</p>
		<div class="postText">
				<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: #000000">    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #000000">iostream</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    #include </span>
						<span style="COLOR: #000000">&lt;</span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000">&gt;</span>
						<span style="COLOR: #000000">    </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> 必须包含这个头文件</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">const</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> chunk </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">16</span>
						<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">class</span>
						<span style="COLOR: #000000"> Foo<br /><img id="Codehighlighter1_87_99_Open_Image" onclick="this.style.display='none'; Codehighlighter1_87_99_Open_Text.style.display='none'; Codehighlighter1_87_99_Closed_Image.style.display='inline'; Codehighlighter1_87_99_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_87_99_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_87_99_Closed_Text.style.display='none'; Codehighlighter1_87_99_Open_Image.style.display='inline'; Codehighlighter1_87_99_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_87_99_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_87_99_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" /><br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
						</span>
						<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">char</span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> buf </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">[ </span>
						<span style="COLOR: #0000ff">sizeof</span>
						<span style="COLOR: #000000">(Foo) </span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> chunk ];<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">int</span>
						<span style="COLOR: #000000"> main(</span>
						<span style="COLOR: #0000ff">int</span>
						<span style="COLOR: #000000"> argc, </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">*</span>
						<span style="COLOR: #000000"> argv[])<br /><img id="Codehighlighter1_187_290_Open_Image" onclick="this.style.display='none'; Codehighlighter1_187_290_Open_Text.style.display='none'; Codehighlighter1_187_290_Closed_Image.style.display='inline'; Codehighlighter1_187_290_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_187_290_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_187_290_Closed_Text.style.display='none'; Codehighlighter1_187_290_Open_Image.style.display='inline'; Codehighlighter1_187_290_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_187_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_187_290_Open_Text">
								<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> 只有这种形式的创建，没有配对形式的delete </span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        Foo</span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000"> pb </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> (buf) Foo;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        <img src="http://www.cppblog.com/images/dot.gif" /><img src="http://www.cppblog.com/images/dot.gif" />        <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        delete[] buff;<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: #0000ff">return</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">0</span>
								<span style="COLOR: #000000">;<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
						</span>
				</div>
		</div>
<img src ="http://www.cppblog.com/keyws/aggbug/11573.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-22 14:54 <a href="http://www.cppblog.com/keyws/archive/2006/08/22/11573.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>【note】Effective C++ (1) - Introduction </title><link>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html</link><dc:creator>keyws</dc:creator><author>keyws</author><pubDate>Sun, 20 Aug 2006 08:10:00 GMT</pubDate><guid>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html</guid><wfw:comment>http://www.cppblog.com/keyws/comments/11495.html</wfw:comment><comments>http://www.cppblog.com/keyws/archive/2006/08/20/11495.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/keyws/comments/commentRss/11495.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/keyws/services/trackbacks/11495.html</trackback:ping><description><![CDATA[
		<h1 style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">Introduction</span>
		</h1>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">1. A <span style="COLOR: green">declaration</span> tells compilers about the name and type of an object, function, class, or template, but it omits certain details.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">2. A <span style="COLOR: green">definition</span>, on the other hand, provides compilers with the details. For an object, the definition is where compilers allocate memory for the object. For a function or a function template, the definition provides the code body. For a class or a class template, the definition lists the members of the class or template.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">3. When you define a class, you generally need a default constructor if you want to define arrays of objects.Incidentally, if you want to create an array of objects for which there is no default constructor, the usual ploy is to define an array of pointers instead. Then you can initialize each pointer separately by using new.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">4. Probably the most important use of the copy constructor is to define what it means to pass and return objects by value.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 7.8pt 0cm; mso-para-margin-top: .5gd; mso-para-margin-right: 0cm; mso-para-margin-bottom: .5gd; mso-para-margin-left: 0cm">
				<span lang="EN-US">5. From a purely operational point of view, the difference between initialization and assignment is that the former is performed by a constructor while the latter is performed by operator=. In other words, the two processes correspond to different function calls. The reason for the distinction is that the two kinds of functions <span style="COLOR: green">must worry about different things</span>. Constructors usually have to check their arguments for validity, whereas most assignment operators can take it for granted that their argument is legitimate (because it has already been constructed). On the other hand, the target of an assignment, unlike an object undergoing construction, may already have resources allocated to it. These resources typically must be released before the new resources can be assigned. Frequently, one of these resources is memory. Before an assignment operator can allocate memory for a new value, it must first deallocate the memory that was allocated for the old value.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
		</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: #008000">//</span>
				<span style="COLOR: #008000"> a possible String constructor</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">String::String(</span>
				<span style="COLOR: #0000ff">const</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #0000ff">char</span>
				<span style="COLOR: #000000"> </span>
				<span style="COLOR: #000000">*</span>
				<span style="COLOR: #000000">value)<br /><img id="Codehighlighter1_67_287_Open_Image" onclick="this.style.display='none'; Codehighlighter1_67_287_Open_Text.style.display='none'; Codehighlighter1_67_287_Closed_Image.style.display='inline'; Codehighlighter1_67_287_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_67_287_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_67_287_Closed_Text.style.display='none'; Codehighlighter1_67_287_Open_Image.style.display='inline'; Codehighlighter1_67_287_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_67_287_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_67_287_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> (value)<br /><img id="Codehighlighter1_82_175_Open_Image" onclick="this.style.display='none'; Codehighlighter1_82_175_Open_Text.style.display='none'; Codehighlighter1_82_175_Closed_Image.style.display='inline'; Codehighlighter1_82_175_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_82_175_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_82_175_Closed_Text.style.display='none'; Codehighlighter1_82_175_Open_Image.style.display='inline'; Codehighlighter1_82_175_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_82_175_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_82_175_Open_Text">
								<span style="COLOR: #000000">{ <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> if value ptr isn't null</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">[strlen(value) </span>
								<span style="COLOR: #000000">+</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        strcpy(data,value);<br /><img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">else</span>
						<span style="COLOR: #000000"> <br /><img id="Codehighlighter1_186_285_Open_Image" onclick="this.style.display='none'; Codehighlighter1_186_285_Open_Text.style.display='none'; Codehighlighter1_186_285_Closed_Image.style.display='inline'; Codehighlighter1_186_285_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align="top" /><img id="Codehighlighter1_186_285_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_186_285_Closed_Text.style.display='none'; Codehighlighter1_186_285_Open_Image.style.display='inline'; Codehighlighter1_186_285_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif" align="top" />    </span>
						<span id="Codehighlighter1_186_285_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_186_285_Open_Text">
								<span style="COLOR: #000000">{ <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> handle null value ptr3</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">new</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">[</span>
								<span style="COLOR: #000000">1</span>
								<span style="COLOR: #000000">];<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000">data </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">\0</span>
								<span style="COLOR: #000000">'</span>
								<span style="COLOR: #000000">; </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> add trailing</span>
								<span style="COLOR: #008000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
								</span>
								<span style="COLOR: #000000">        </span>
								<span style="COLOR: #0000ff">null</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000">
										<br />
										<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align="top" />    }</span>
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
				</span>
				<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: #008000">//</span>
				<span style="COLOR: #008000"> a possible String assignment operator</span>
				<span style="COLOR: #008000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
				</span>
				<span style="COLOR: #000000">
						<br />
						<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />String</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> String::</span>
				<span style="COLOR: #0000ff">operator</span>
				<span style="COLOR: #000000">=</span>
				<span style="COLOR: #000000">(</span>
				<span style="COLOR: #0000ff">const</span>
				<span style="COLOR: #000000"> String</span>
				<span style="COLOR: #000000">&amp;</span>
				<span style="COLOR: #000000"> rhs)<br /><img id="Codehighlighter1_377_593_Open_Image" onclick="this.style.display='none'; Codehighlighter1_377_593_Open_Text.style.display='none'; Codehighlighter1_377_593_Closed_Image.style.display='inline'; Codehighlighter1_377_593_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" /><img id="Codehighlighter1_377_593_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_377_593_Closed_Text.style.display='none'; Codehighlighter1_377_593_Open_Image.style.display='inline'; Codehighlighter1_377_593_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" /></span>
				<span id="Codehighlighter1_377_593_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_377_593_Open_Text">
						<span style="COLOR: #000000">{<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    </span>
						<span style="COLOR: #0000ff">if</span>
						<span style="COLOR: #000000"> (</span>
						<span style="COLOR: #0000ff">this</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">==</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">&amp;</span>
						<span style="COLOR: #000000">rhs)<br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />        </span>
						<span style="COLOR: #0000ff">return</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: #008000">//</span>
						<span style="COLOR: #008000"> see Item 17</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    delete [] data; </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> delete old memory</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">    <br /><img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    data </span>
						<span style="COLOR: #000000">=</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #008000">//</span>
						<span style="COLOR: #008000"> allocate new memory</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />
						</span>
						<span style="COLOR: #000000">        </span>
						<span style="COLOR: #0000ff">new</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #0000ff">char</span>
						<span style="COLOR: #000000">[strlen(rhs.data) </span>
						<span style="COLOR: #000000">+</span>
						<span style="COLOR: #000000"> </span>
						<span style="COLOR: #000000">1</span>
						<span style="COLOR: #000000">];<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" />    strcpy(data, rhs.data);<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: #0000ff">return</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: #008000">//</span>
						<span style="COLOR: #008000"> see Item 15</span>
						<span style="COLOR: #008000">
								<br />
								<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />
						</span>
						<span style="COLOR: #000000">}</span>
				</span>
		</div>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<br />6. These different casting forms serve different purposes:</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">const_cast</span>
				<span lang="EN-US"> is designed to cast away the constness of objects and pointers, a topic I examine in Item 21.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">dynamic_cast</span>
				<span lang="EN-US"> is used to perform "safe downcasting," a subject we'll explore in Item 39.</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">reinterpret_cast</span>
				<span lang="EN-US"> is engineered for casts that yield <span style="COLOR: green">implementation-dependent results</span>, e.g., casting between function pointer types. (You're not likely to need reinterpret_cast very often. I don't use it at all in this book.)</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="COLOR: green">static_cast </span>
				<span lang="EN-US">is sort of the catch-all cast. It's what you use when none of the other casts is appropriate. <span style="COLOR: green">It's the closest in meaning to the conventional C-style casts.</span></span>
		</p>
<img src ="http://www.cppblog.com/keyws/aggbug/11495.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/keyws/" target="_blank">keyws</a> 2006-08-20 16:10 <a href="http://www.cppblog.com/keyws/archive/2006/08/20/11495.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>