﻿<?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>博客园-Jinhao::天水涧</title><link>http://www.cnblogs.com/jinhao/</link><description>天水涧 一线之间</description><language>zh-cn</language><lastBuildDate>Mon, 06 Jul 2009 05:06:31 GMT</lastBuildDate><pubDate>Mon, 06 Jul 2009 05:06:31 GMT</pubDate><ttl>60</ttl><item><title>混沌 IN C++::Template Metaprograms</title><link>http://www.cnblogs.com/jinhao/archive/2004/06/29/19372.html</link><dc:creator>天水涧</dc:creator><author>天水涧</author><pubDate>Mon, 28 Jun 2004 21:12:00 GMT</pubDate><guid>http://www.cnblogs.com/jinhao/archive/2004/06/29/19372.html</guid><wfw:comment>http://www.cnblogs.com/jinhao/comments/19372.html</wfw:comment><comments>http://www.cnblogs.com/jinhao/archive/2004/06/29/19372.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cnblogs.com/jinhao/comments/commentRss/19372.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/jinhao/services/trackbacks/19372.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#0000ff><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">难度</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">:<IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"></FONT></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">文前说明：文中涉及到的观点并不是要求你去遵循它，而本文只相当于一篇&#8220;科普文章&#8221;。其中的关于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的语法在这里就不过多介绍了。例如，下文中提到的条件，都是要意识到是编译期常量。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">为我们提供了编译期计算的功能。编译器在处理</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">时，实际上相当于一个解释器的作用。充分利用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">可以提高代码的运行速度，可以降低代码维护的复杂度，还可以为代码提供更具扩展性的能力。下面就尝试一下这样的编程方式。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore"><FONT face="Courier New">一、</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">替代</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">if/if-else</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">if</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">(</SPAN></FONT><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">条件</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">) <o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-INDENT: 13.5pt; mso-char-indent-count: 1.5; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18.05pt; mso-char-indent-count: 2.0"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">else <o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 27pt; mso-char-indent-count: 3.0"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中，我们可以这样写</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">bool</B> cond&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_if{};<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_if&lt;<B style="mso-bidi-font-weight: normal">true</B>&gt;{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.6pt; mso-char-indent-count: 3.5"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">static</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> <B style="mso-bidi-font-weight: normal">void</B> go()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{ </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1; }<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_if&lt;<B style="mso-bidi-font-weight: normal">false</B>&gt;{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.6pt; mso-char-indent-count: 3.5"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">static</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> <B style="mso-bidi-font-weight: normal">void</B> go()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{ </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2; }<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">例如，我们判断两个整数</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">my_if&lt;(1&lt;5)&gt;::go(); <o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">也许你会觉得这个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">if/if-else</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">有点大提小做的感觉，呵呵，继续往下看</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore"><FONT face="Courier New">二、</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">将</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">版的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">if/if-else</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">扩展成能应付</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的代码</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">如果你是一个面向对象编程的高手，你肯定不会在你的代码中安置</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这样的代码，你肯定会用通过抽象、继承、多态和接口来完成这个任务。不过现在要换一个思维，用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">来实现。其实这里的代码和上面的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">if/if-else</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">相差无几，但是在用于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">时，也同样能出色地体现出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">OC</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">原则。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> i;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">switch</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">(i)<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">case</B> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><SPAN style="mso-spacerun: yes"><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">break</B>;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">case</B> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><SPAN style="mso-spacerun: yes"><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">break</B>;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">default</B>:<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><SPAN style="mso-spacerun: yes"><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">3;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">下面是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">版本</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">int</B> I&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_switch{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">static</B> <B style="mso-bidi-font-weight: normal">void</B> go(){<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><SPAN style="mso-spacerun: yes"><FONT face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">3;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 36pt; mso-char-indent-count: 4.0"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_switch&lt;</SPAN></FONT><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1&gt;{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.6pt; mso-char-indent-count: 3.5"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">static</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> <B style="mso-bidi-font-weight: normal">void</B> go()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{ </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">1; }<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> my_switch &lt;</SPAN></FONT><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2&gt;{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.6pt; mso-char-indent-count: 3.5"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">static</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> <B style="mso-bidi-font-weight: normal">void</B> go()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 31.5pt; mso-char-indent-count: 3.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{ </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">语句</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">2; }<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">调用就是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">my_switch&lt;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">&gt;::go();<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; TEXT-INDENT: 0.05pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">你也许仍然找不出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">版本的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">好处，因为它要写更多的代码。不过你现在静下来认真的想一想，你会发现当你为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">插入新的值判断时，你要返回到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">处，而且还要修改内部的代码。而用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">却不需要你去关心</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">my_switch</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">到底在哪里定义的。要添加新的值判断时，只需要你自己在一个合适的地方添加值判断的代码。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore"><FONT face="Courier New">三、</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">基于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++ template</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的数值运算</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">计算一个常数</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">N</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的阶乘</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">int</B> N&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> factorial{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">static const int</B> value = N * factorial&lt;N-<SPAN style="COLOR: purple">1</SPAN>&gt;::value;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> factorial&lt;<SPAN style="COLOR: purple">1</SPAN>&gt;{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">static const int</B> value = <SPAN style="COLOR: purple">1</SPAN>;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">当这个类模板实例化</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">factorial&lt;N&gt;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，就会接着实例化</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">factorial&lt;N-1&gt;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，直到实例化</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">fatorial&lt;1&gt;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">为止。而你只需要实例化自己想要计算的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">N</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就行了，后面的一切全由编译器包办。例如我们要输出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">10</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的阶乘答案</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">std::cout&lt;&lt;factorial&lt;10&gt;&lt;&lt;std::endl;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt; mso-list: l0 level1 lfo1; tab-stops: list 21.0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; mso-bidi-font-family: 宋体"><SPAN style="mso-list: Ignore"><FONT face="Courier New">四、</FONT><SPAN style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">借助上面数值计算的方法以产生更实用的代码</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是否考虑过得到指针的原型，比如通过</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int*</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，得到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">。我们可以写出下面的代码</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> primitive_type{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">typedef</B> T value_type;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> primitive_type&lt;T*&gt;{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">typedef</B> T value_type;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">typedef</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> int* pint;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">primitive_type&lt;pint&gt;::value_type obj=5;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">std::cout&lt;&lt;obj&lt;&lt;std::endl;<BR><BR><o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">现在可以明确</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">obj</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">不是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int*</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，而是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型。但是有个缺陷就是但</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">T</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int**</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">时却的不到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，而是得到的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int*</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，这并不是我们想要的答案。现在只需要其实稍微对</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">primitive_type</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的偏特化版本作下简单的修改，就可以满足要求</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> primitive_type&lt;T*&gt;{<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><B style="mso-bidi-font-weight: normal">typedef</B> <B style="mso-bidi-font-weight: normal">typename</B> primitive_type&lt;T&gt;::value_type value_type;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">typedef</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> <B style="mso-bidi-font-weight: normal">int</B>**** pint;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt 17.95pt; mso-para-margin-left: 1.71gd"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">primitive_type&lt;pint&gt;::value_type obj=5;<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">obj</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">可以确认他是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，而不是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int***<o:p></o:p></FONT></SPAN></P><img src ="http://www.cnblogs.com/jinhao/aggbug/19372.html?type=1" width = "1" height = "1" /><br/><br/>--------------------------<br/>新闻：<a href="http://news.cnblogs.com/n/48002/" target="_blank">竞争日趋激烈 微软欲借 Windows 7 扭转战局</a><br/>网站导航: <a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻</a>&nbsp;&nbsp;<a href="http://dotnet.cnblogs.com" target="_blank">.NET频道</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/q/" target="_blank">博问</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/ing/" target="_blank">闪存</a>&nbsp;&nbsp;<a href="http://zzk.cnblogs.com" target="_blank">找找看</a>]]></description></item><item><title>混沌 IN C++::模板参数的奥秘</title><link>http://www.cnblogs.com/jinhao/archive/2004/06/08/14095.html</link><dc:creator>天水涧</dc:creator><author>天水涧</author><pubDate>Mon, 07 Jun 2004 21:35:00 GMT</pubDate><guid>http://www.cnblogs.com/jinhao/archive/2004/06/08/14095.html</guid><wfw:comment>http://www.cnblogs.com/jinhao/comments/14095.html</wfw:comment><comments>http://www.cnblogs.com/jinhao/archive/2004/06/08/14095.html#Feedback</comments><slash:comments>7</slash:comments><wfw:commentRss>http://www.cnblogs.com/jinhao/comments/commentRss/14095.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/jinhao/services/trackbacks/14095.html</trackback:ping><description><![CDATA[<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">难度</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue"><FONT face="Courier New">:<IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">先来一道思考题</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T, T* p&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> A<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">假若有个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">int</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型的对象</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">i</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，那么对于下面这段代码</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">A&lt;<B style="mso-bidi-font-weight: normal">int</B>, &amp;i&gt; obj;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这个是合法的吗？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">答：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New"> A&lt;<B style="mso-bidi-font-weight: normal">int</B>, &amp;i&gt; obj; </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">可能合法</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">或</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">可能不合法。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">什么东西可以当作模板的参数呢？</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">部分的内建类型和用户类型，和部分非类型的东西也可以当作模板参数。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">非类型模板参数的一个要求是，编译器能在编译期就能把参数确定下来。换言之，就是非类型的模板参数必须是个编译期常量。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">判断这句是否合法得看</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">&amp;i</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">返回的是不是一个编译期常量。当</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">i</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是全局或静态对象，那么这个语句就是正确的，因为全局和静态对象的内存分配发生在编译期，所以这样一来</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">i</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的地址</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">(&amp;i</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的值</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">)</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就是可以被确定的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">现在把这个程序补全成合法的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> i;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> main(){<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22.5pt; mso-char-indent-count: 2.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">A&lt;int, &amp;i&gt; obj;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">如果这个模板的第二个参数是引用，那么也是同理。不过值得注意的是，这些非类型、非引用模板参数都不是左值！</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">最后，可以当作非类型参数的东西有</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">整数、</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">enum</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型、指针、引用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">其中局部的用户自定义类型</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">(<B style="mso-bidi-font-weight: normal">Local Class</B>)</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">不能作为模板参数。这是因为局部类没有外部连接。举个例子</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">class</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> TEST{};<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">void</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> fun1()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> X{};<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">TEST&lt;X&gt; a;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p><FONT face="Courier New">&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">void</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> fun2()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt"><FONT face="Courier New"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt"> X{};<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 18pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">TEST&lt;X&gt; a;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">上面的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">TEST&lt;X&gt; a;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是同一个东西吗？</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">由于没有外部连接，它们就是同一个东西，而程序员的本意是两个局部类</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">X</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是两个不同的类定义，也认为</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">TEST&lt;A&gt;</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是两个不同的模板实例。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">对于局部类，可以说它是</SPAN><SPAN style="FONT-SIZE: 9pt"><FONT face="Courier New"> </FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">健全的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">C++</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">类型系统的一个畸形儿。没有外部连接导致它不能拥有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">static data members</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，不能拥有</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">template member functions</FONT></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">等等。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt"><FONT face="Courier New">//The End</FONT></SPAN></B></SPAN></P><img src ="http://www.cnblogs.com/jinhao/aggbug/14095.html?type=1" width = "1" height = "1" /><br/><br/>--------------------------<br/>新闻：<a href="http://news.cnblogs.com/n/48001/" target="_blank">上海电信计划2012年80%用户实现100M带宽</a><br/>网站导航: <a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻</a>&nbsp;&nbsp;<a href="http://dotnet.cnblogs.com" target="_blank">.NET频道</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/q/" target="_blank">博问</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/ing/" target="_blank">闪存</a>&nbsp;&nbsp;<a href="http://zzk.cnblogs.com" target="_blank">找找看</a>]]></description></item><item><title>混沌 IN C++::动态资源管理陷阱</title><link>http://www.cnblogs.com/jinhao/archive/2004/06/06/13828.html</link><dc:creator>天水涧</dc:creator><author>天水涧</author><pubDate>Sun, 06 Jun 2004 12:16:00 GMT</pubDate><guid>http://www.cnblogs.com/jinhao/archive/2004/06/06/13828.html</guid><wfw:comment>http://www.cnblogs.com/jinhao/comments/13828.html</wfw:comment><comments>http://www.cnblogs.com/jinhao/archive/2004/06/06/13828.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnblogs.com/jinhao/comments/commentRss/13828.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/jinhao/services/trackbacks/13828.html</trackback:ping><description><![CDATA[<SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#0000ff><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">难度</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">:<IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">先阅读下面的代码然后来解答下面的那个问题</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><?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; TEXT-INDENT: 18pt; mso-char-indent-count: 2.0"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> A<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">{};<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> main()<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">{<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 13.5pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A *p = <B style="mso-bidi-font-weight: normal">new</B> A;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 13.5pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">p-&gt;~A();<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 13.5pt; mso-char-indent-count: 1.5"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">free(p);<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">问题：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'">new/delete </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">和 </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'">malloc/free </SPAN><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">有哪些区别？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">答：</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">new/delete </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">会在分配的内存上调用对象的构造函数和析构函数来初始化和销毁对象，而 </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">malloc/free </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">却不能。更重要的是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">new/delete </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">和 </SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">malloc/free </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">不能混用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">new </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是在自由存储区中分配内存，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">delete </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是负责释放自由存储区中的内存</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">malloc </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是在堆中分配内存，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">free </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">则负责释放。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">自由存储区和堆的访问方式是不同的，或者说是与平台相关的，如果混用那么必将导致程序崩溃。在某些编译器上也许不会出现问题，但永远相信这是错误的、不可移植的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">现在，上面那段代码有问题吗？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"></SPAN>&nbsp;</P>
<P></P><img src ="http://www.cnblogs.com/jinhao/aggbug/13828.html?type=1" width = "1" height = "1" /><br/><br/>--------------------------<br/>新闻：<a href="http://news.cnblogs.com/n/48000/" target="_blank">数万名网友签名抗议星际争霸2取消局域网功能</a><br/>网站导航: <a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻</a>&nbsp;&nbsp;<a href="http://dotnet.cnblogs.com" target="_blank">.NET频道</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/q/" target="_blank">博问</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/ing/" target="_blank">闪存</a>&nbsp;&nbsp;<a href="http://zzk.cnblogs.com" target="_blank">找找看</a>]]></description></item><item><title>混沌 In C++::是类型？还是函数调用？</title><link>http://www.cnblogs.com/jinhao/archive/2004/06/05/13615.html</link><dc:creator>天水涧</dc:creator><author>天水涧</author><pubDate>Sat, 05 Jun 2004 08:58:00 GMT</pubDate><guid>http://www.cnblogs.com/jinhao/archive/2004/06/05/13615.html</guid><wfw:comment>http://www.cnblogs.com/jinhao/comments/13615.html</wfw:comment><comments>http://www.cnblogs.com/jinhao/archive/2004/06/05/13615.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://www.cnblogs.com/jinhao/comments/commentRss/13615.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/jinhao/services/trackbacks/13615.html</trackback:ping><description><![CDATA[<FONT face="Courier New" size=2><FONT size=5><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">难度</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">:<IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"><IMG src="http://www.cnblogs.com/Emoticons/star.gif"></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">文前提醒：看这篇文章时须家长陪同并引导，以免走火入魔</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><?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 style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">先看看下面的代码？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> A<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>A(){}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">template</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">&lt;<B style="mso-bidi-font-weight: normal">typename</B> T&gt;<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">void</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> func(T() )<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>{}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">int</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> main()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>A a( A() );<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp; </SPAN></FONT><SPAN style="COLOR: #666699">//(1), OK</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>func( A() );<SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN></FONT><SPAN style="COLOR: #666699">//(2), Wrong</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp; </SPAN>a = </FONT><SPAN style="COLOR: purple">5</SPAN><FONT color=#000000>;<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="mso-spacerun: yes">&nbsp;</SPAN></FONT><SPAN style="COLOR: #666699">//(3), Wrong</SPAN><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(1)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">、</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> <SPAN lang=EN-US>A<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>a( A() ); </SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是什么意思？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">在这里并不是用</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">创建一个对象，然后实例化对象</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">a</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">。这里真正的语</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">义</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">a</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是一个参数为<B style="mso-bidi-font-weight: normal">返回</B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">对象的无参函数指针</SPAN></B><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">的函数，所以它的真面目应该是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">A a( A (*)() ) </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">。而这句仅仅起到声明函数的作用。重点就在于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">并不是创建对象，而是一个<B style="mso-bidi-font-weight: normal">无参的、返回为</B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">A</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">的无名函数</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">为什么说</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">A() </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">是一个无名的函数呢？</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"> </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">来类比一下</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">一个普通函数应该这样写</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"> A fun()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">现在它是无名的，那么把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">fun</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">去掉</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"> </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">就成了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">A()<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">(2)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">、</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"> <SPAN lang=EN-US>func( A() ); </SPAN></SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'">为什么会出错呢？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这里虽然函数模板</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">func</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的参数同样也是一个<B style="mso-bidi-font-weight: normal">无参的、返回为</B></SPAN><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">T</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">的函数指针</SPAN></B><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，那为什么会出错呢？</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">其实在这里</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就不是一个无名函数了，而是创建一个临时对象。那么这个函数即成了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> void func( A )</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，而原本的类型是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">void func( T (*)() )</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，很明显参数类型不匹配。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(3)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">、</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> a = 5; </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">现在应该没有任何疑问了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">int </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">传递给</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> A (*)( A (*)() )</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，地球人都知道是错的。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">千万别走火入魔！</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是什么导致了</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(1)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">和</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(2)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">中的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">表现出不同的语义呢？</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">答：</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">注意</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(1)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">是声明，当</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">这个具有二意性东西出现在声明或定义中，那么它都被看作是&#8220;类型&#8221;而不是函数调用。所以这样一来，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">(2)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">明显不是定义，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A()</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就被解析成调用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> <o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p><FONT color=#000000>&nbsp;</FONT></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">更多</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">struct</SPAN></B><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"> A<o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000><SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>A(<B style="mso-bidi-font-weight: normal">int</B> ){}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>};<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 'Courier New'"><o:p>&nbsp;</o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT color=#000000><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A a(A(1)); </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">在这里</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A(1) </SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就不可能是&#8220;类型&#8221;了，因为其中有个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">1</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">，由于</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">1</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">不是&#8220;类型&#8221;，所以</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'">A(1)</SPAN><SPAN style="FONT-SIZE: 9pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'">就成了函数调用。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><o:p></o:p></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-SIZE: 9pt; FONT-FAMILY: 'Courier New'"><FONT color=#000000>//The End<o:p></o:p></FONT></SPAN></B></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"></SPAN></SPAN></FONT><SPAN style="FONT-SIZE: 9pt; COLOR: blue; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'"><FONT face=宋体><SPAN style="FONT-SIZE: 9pt; COLOR: blue; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-hansi-font-family: 'Times New Roman'"></SPAN></FONT></SPAN></FONT></P><img src ="http://www.cnblogs.com/jinhao/aggbug/13615.html?type=1" width = "1" height = "1" /><br/><br/>--------------------------<br/>新闻：<a href="http://news.cnblogs.com/n/47999/" target="_blank">Silverlight打造杰克逊纪念专题</a><br/>网站导航: <a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻</a>&nbsp;&nbsp;<a href="http://dotnet.cnblogs.com" target="_blank">.NET频道</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/q/" target="_blank">博问</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/ing/" target="_blank">闪存</a>&nbsp;&nbsp;<a href="http://zzk.cnblogs.com" target="_blank">找找看</a>]]></description></item><item><title>跟踪源码 调试无忧</title><link>http://www.cnblogs.com/jinhao/archive/2004/06/05/13506.html</link><dc:creator>天水涧</dc:creator><author>天水涧</author><pubDate>Fri, 04 Jun 2004 19:37:00 GMT</pubDate><guid>http://www.cnblogs.com/jinhao/archive/2004/06/05/13506.html</guid><wfw:comment>http://www.cnblogs.com/jinhao/comments/13506.html</wfw:comment><comments>http://www.cnblogs.com/jinhao/archive/2004/06/05/13506.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnblogs.com/jinhao/comments/commentRss/13506.html</wfw:commentRss><trackback:ping>http://www.cnblogs.com/jinhao/services/trackbacks/13506.html</trackback:ping><description><![CDATA[<P><FONT face="Courier New"><FONT size=2>难度:<IMG src="http://www.cnblogs.com/Emoticons/star.gif"><BR>C/C++有三个预编译常量，合理的使用可以为我们简化调试<BR><BR>一: <STRONG>__FILE__</STRONG><BR>&nbsp; 现在有个叫main.cpp的文件，里面有如下的代码<BR><FONT color=#008000>#include&lt;iostream&gt;<BR></FONT><BR><STRONG>int</STRONG> main()<BR>{<BR>&nbsp;&nbsp; std::cout&lt;&lt;__FILE__&lt;&lt;std::endl;<BR>}<BR>运行结果就是在屏幕上显示<BR>&lt;路径&gt;\main.cpp<BR><BR>二: <STRONG>__LINE__</STRONG><BR>只用代码说话<BR><FONT color=#006400>#include&lt;iostream&gt;<BR></FONT><BR><STRONG>int</STRONG> main()<BR>{<BR>&nbsp;&nbsp; std::cout&lt;&lt;__LINE__&lt;&lt;std::endl;<BR>}<BR>输出就是，__LINE__所在的代码行数<BR><BR>三: <STRONG>__FUNCTION__(非标准)</STRONG><BR>我最后一次强调，还是用代码说话<BR><FONT color=#006400>#include&lt;iostream&gt;</FONT><BR><BR><STRONG>int</STRONG> main()<BR>{<BR>&nbsp;&nbsp; std::cout&lt;&lt;__FUNCTION__&lt;&lt;std::endl;<BR>}<BR>输出就是<BR>main<BR>这东西就是__FUNCTION__所在函数的函数名<BR>-----------------------------------------<BR>这三个玩意儿的应用<BR>方便调试程序，例如想写一个内嵌到代码里的内存泄露检测工具，就用这东西可以把代码何处的泄漏都毫不保留的说出来。当然别指望源代码级这个检测工具很强大，这由C++的语法决定了它不能太强大。<BR><STRONG>//The End</STRONG></FONT></FONT></P><img src ="http://www.cnblogs.com/jinhao/aggbug/13506.html?type=1" width = "1" height = "1" /><br/><br/>--------------------------<br/>新闻：<a href="http://news.cnblogs.com/n/47999/" target="_blank">Silverlight打造杰克逊纪念专题</a><br/>网站导航: <a href="http://www.cnblogs.com" target="_blank">博客园首页</a>&nbsp;&nbsp;<a href="http://news.cnblogs.com" target="_blank">新闻</a>&nbsp;&nbsp;<a href="http://dotnet.cnblogs.com" target="_blank">.NET频道</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com" target="_blank">社区</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/q/" target="_blank">博问</a>&nbsp;&nbsp;<a href="http://space.cnblogs.com/ing/" target="_blank">闪存</a>&nbsp;&nbsp;<a href="http://zzk.cnblogs.com" target="_blank">找找看</a>]]></description></item></channel></rss>