﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Smart Pointer</title><link>http://www.cppblog.com/xx1206917580/</link><description /><language>zh-cn</language><lastBuildDate>Sun, 05 Apr 2026 16:25:17 GMT</lastBuildDate><pubDate>Sun, 05 Apr 2026 16:25:17 GMT</pubDate><ttl>60</ttl><item><title>杂记</title><link>http://www.cppblog.com/xx1206917580/archive/2011/11/01/159469.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Tue, 01 Nov 2011 10:22:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/11/01/159469.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/159469.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/11/01/159469.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/159469.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/159469.html</trackback:ping><description><![CDATA[<pre id="content-87825405"  mb10"="" style="margin-top: 0px; margin-bottom: 10px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial; white-space: pre-wrap; word-wrap: break-word; zoom: 1; line-height: 24px; background-color: #fdfff8; "><span style="font-family: Courier; ">关于C++函数声明的问题 ：</span><span style="font-family: Courier; ">加上｛｝后就不能算是声明了，而是已经定义了一个函数！</span></pre><img src ="http://www.cppblog.com/xx1206917580/aggbug/159469.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-11-01 18:22 <a href="http://www.cppblog.com/xx1206917580/archive/2011/11/01/159469.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++引用与指针的比较 </title><link>http://www.cppblog.com/xx1206917580/archive/2011/08/06/152660.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Sat, 06 Aug 2011 11:34:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/08/06/152660.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/152660.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/08/06/152660.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/152660.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/152660.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">引用是C++中的概念，初学者容易把引用和指针混淆一起。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">一下程序中，n是m的一个引用（reference），m是被引用物（referent）。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int m;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int &amp;n = m;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">n相当于m的别名（绰号），对n的任何操作就是对m的操作。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">所以n既不是m的拷贝，也不是指向m的指针，其实n就是m它自己。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">引用的规则：&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">（1）引用被创建的同时必须被初始化（指针则可以在任何时候被初始化）。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">（2）不能有NULL引用，引用必须与合法的存储单元关联（指针则可以是NULL）。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">（3）一旦引用被初始化，就不能改变引用的关系（指针则可以随时改变所指的对象）。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">以下示例程序中，k被初始化为i的引用。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">语句k = j并不能将k修改成为j的引用，只是把k的值改变成为6。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">由于k是i的引用，所以i的值也变成了6。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int i = 5;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int j = 6;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int &amp;k = i;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">k = j; // k和i的值都变成了6;&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">引用的主要功能是传递函数的参数和返回值。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">C++语言中，函数的参数和返回值的传递方式有三种：值传递、指针传递和引用传递。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">以下是"值传递"的示例程序。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">由于Func1函数体内的x是外部变量n的一份拷贝，改变x的值不会影响n, 所以n的值仍然是0。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">void Func1(int x)&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">{&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">x = x + 10;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">}&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">...&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int n = 0;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">Func1(n);&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">cout &lt;&lt; "n = " &lt;&lt; n &lt;&lt; endl; // n = 0&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">以下是"指针传递"的示例程序。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">由于Func2函数体内的x是指向外部变量n的指针，改变该指针的内容将导致n的值改变，所以n的值成为10。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">void Func2(int *x)&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">{&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">(* x) = (* x) + 10;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">}&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">...&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int n = 0;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">Func2(&amp;n);&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">cout &lt;&lt; "n = " &lt;&lt; n &lt;&lt; endl; // n = 10&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">以下是"引用传递"的示例程序。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">由于Func3函数体内的x是外部变量n的引用，x和n是同一个东西，改变x等于改变n，所以n的值成为10。&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">void Func3(int &amp;x)&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">{&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">x = x + 10;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">}&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">...&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">int n = 0;&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">Func3(n);&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">cout &lt;&lt; "n = " &lt;&lt; n &lt;&lt; endl; // n = 10&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">对比上述三个示例程序，会发现"引用传递"的性质象"指针传递"，而书写方式象"值传递"。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">实际上"引用"可以做的任何事情"指针"也都能够做，为什么还要"引用"这东西？&nbsp;<br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">答案是"用适当的工具做恰如其分的工作"。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">指针能够毫无约束地操作内存中的任何东西，尽管指针功能强大，但是非常危险。&nbsp;<br /><br /></span><span class="Apple-style-span" style="line-height: normal; background-color: #ffffff; font-family: Courier; font-size: 10pt; ">如果的确只需要借用一下某个对象的"别名"，那么就用"引用"，而不要用"指针"，以免发生意外。</span><img src ="http://www.cppblog.com/xx1206917580/aggbug/152660.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-08-06 19:34 <a href="http://www.cppblog.com/xx1206917580/archive/2011/08/06/152660.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>OSG节点访问和遍历</title><link>http://www.cppblog.com/xx1206917580/archive/2011/08/05/152515.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Fri, 05 Aug 2011 02:39:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/08/05/152515.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/152515.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/08/05/152515.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/152515.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/152515.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Simsun; font-size: 12px; line-height: normal; background-color: #ffffff; "><p><span style="font-family: Courier; ">节点访问：</span></p><p><span style="font-family: Courier; ">　　OSG中节点的访问使用的是一种访问器模式。</span></p><p><span style="font-family: Courier; ">　　一个典型的访问器涉及抽象访问者角色(Visitor), 具体访问者(Concrete Visitor), 节点角色(Node)。</span></p><p><span style="font-family: Courier; ">　　OSG中访问者角色为NodeVisitor类，其基本结构如下：</span></p><p><span style="font-family: Courier; ">　　NodeVisitor(TraversalMode tm)&nbsp;&nbsp;&nbsp; //构造函数，TraversalMode为节点树的遍历方式</span></p><p><span style="font-family: Courier; ">　　//TRAVERSE_NONE, 仅当前节点</span></p><p><span style="font-family: Courier; ">　　//TRAVERSE_PARENTS, 向当前节点的父节点遍历</span></p><p><span style="font-family: Courier; ">　　//TRAVERSE_ALL_CHILDREN, 向子节点遍历</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;traverse(Node&amp; node)　 //向下一个需要访问的节点推进</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;apply(Node&amp; node)　　　//虚函数，访问各种节点类型，并执行访问器中的自定义操作</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;apply(Group&amp; node)</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;apply(Geode&amp; node)</span></p><p><span style="font-family: Courier; ">　　&#8230;&#8230;&#8230;&#8230;</span></p><p><span style="font-family: Courier; ">　　NodeVisitor 只是访问器角色的抽象接口，要使用访问器访问节点并执行自定义操作时，需要继承并重写</span></p><p><span style="font-family: Courier; ">　　apply(&#8230;&#8230;)函数实现自定义功能。osg::Node类中的访问接口为&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;accept(NodeVisitor&amp; nv)。对节点</span></p><p><span style="font-family: Courier; ">　　的访问从节点接受一个访问器开始，将一个具体的访问器对象传递给节点，节点反过来执行访问器的apply(...)</span></p><p><span style="font-family: Courier; ">　　函数，并将自己传入访问器。可如下简单表示：</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;Node::accept(NodeVisitor&amp; nv)</span></p><p><span style="font-family: Courier; ">　　{</span></p><p><span style="font-family: Courier; ">　　nv.apply(*ths) ;</span></p><p><span style="font-family: Courier; ">　　}</span></p><p><span style="font-family: Courier; ">　　遍历节点树：</span></p><p><span style="font-family: Courier; ">　　osg::Node类中有两个辅助函数：</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;ascend(NodeVisitor&amp; nv)&nbsp;&nbsp;&nbsp;&nbsp; //虚函数，向上一级节点推进访问器</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;traverse(NodeVisitor&amp; nv)&nbsp;&nbsp; //虚函数，向下一级节点推进访问器</span></p><p><span style="font-family: Courier; ">　　NodeVisitor的traverse()函数实现如下：</span></p><p><span style="font-family: Courier; ">　　inline&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;traverse(Node&amp; node)</span></p><p><span style="font-family: Courier; ">　　{</span></p><p><span style="font-family: Courier; ">　　if (_traversalMode == TRAVERSE_PARENTS)</span></p><p><span style="font-family: Courier; ">　　node.ascend(*</span><span class="FBlue" style="color: blue; font-family: Courier; ">this</span><span style="font-family: Courier; ">);</span></p><p>　　<span class="FBlue" style="color: blue; font-family: Courier; ">else</span><span style="font-family: Courier; ">&nbsp;if (_traversalMode != TRAVERSE_NONE)</span></p><p><span style="font-family: Courier; ">　　node.traverse(*</span><span class="FBlue" style="color: blue; font-family: Courier; ">this</span><span style="font-family: Courier; ">);</span></p><p><span style="font-family: Courier; ">　　}</span></p><p><span style="font-family: Courier; ">　　示例如下：</span></p><p><span style="font-family: Courier; ">　　代码</span></p><p><span style="font-family: Courier; ">　　1 #include &lt;osg/Node&gt;</span></p><p><span style="font-family: Courier; ">　　2 #include &lt;osgDB/ReadFile&gt;</span></p><p><span style="font-family: Courier; ">　　3 #include &lt;iostream&gt;</span></p><p><span style="font-family: Courier; ">　　4</span></p><p><span style="font-family: Courier; ">　　5&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">using</span>&nbsp;<span class="FBlue" style="color: blue; font-family: Courier; ">namespace</span><span style="font-family: Courier; ">&nbsp;std;</span></p><p><span style="font-family: Courier; ">　　6</span></p><p><span style="font-family: Courier; ">　　7&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">class&nbsp;</span><span style="font-family: Courier; ">InfoVisitor:&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">public</span><span style="font-family: Courier; ">&nbsp;osg::NodeVisitor</span></p><p><span style="font-family: Courier; ">　　8 {</span></p><p><span style="font-family: Courier; ">　　9&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">public</span><span style="font-family: Courier; ">:</span></p><p><span style="font-family: Courier; ">　　10&nbsp;&nbsp;&nbsp;&nbsp; InfoVisitor()</span></p><p><span style="font-family: Courier; ">　　11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :osg::NodeVisitor(TRAVERSE_ALL_CHILDREN), _indent(0)</span></p><p><span style="font-family: Courier; ">　　12&nbsp;&nbsp;&nbsp;&nbsp; {}</span></p><p><span style="font-family: Courier; ">　　13</span></p><p><span style="font-family: Courier; ">　　14&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">virtual</span>&nbsp;<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;apply(osg::Node&amp; node)</span></p><p><span style="font-family: Courier; ">　　15&nbsp;&nbsp;&nbsp;&nbsp; {</span></p><p><span style="font-family: Courier; ">　　16&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">i = 0; i &lt; _indent; i++)&nbsp; cout &lt;&lt; "&nbsp;&nbsp;&nbsp; ";</span></p><p><span style="font-family: Courier; ">　　17&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "[" &lt;&lt; _indent &lt;&lt; "]"&lt;&lt; node.libraryName()</span></p><p><span style="font-family: Courier; ">　　18&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "::" &lt;&lt; node.className() &lt;&lt; endl;</span></p><p><span style="font-family: Courier; ">　　19</span></p><p><span style="font-family: Courier; ">　　20&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _indent++;</span></p><p><span style="font-family: Courier; ">　　21&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; traverse(node);</span></p><p><span style="font-family: Courier; ">　　22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _indent--;</span></p><p><span style="font-family: Courier; ">　　23</span></p><p><span style="font-family: Courier; ">　　24&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">i = 0; i &lt; _indent; i++)&nbsp; cout &lt;&lt; "&nbsp;&nbsp;&nbsp; ";</span></p><p><span style="font-family: Courier; ">　　25&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "[" &lt;&lt; _indent &lt;&lt; "] "&lt;&lt; node.libraryName()</span></p><p><span style="font-family: Courier; ">　　26&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "::" &lt;&lt; node.className() &lt;&lt; endl;</span></p><p><span style="font-family: Courier; ">　　27&nbsp;&nbsp;&nbsp;&nbsp; }</span></p><p><span style="font-family: Courier; ">　　28</span></p><p><span style="font-family: Courier; ">　　29&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">virtual</span>&nbsp;<span class="FBlue" style="color: blue; font-family: Courier; ">void</span><span style="font-family: Courier; ">&nbsp;apply(osg::Geode&amp; node)</span></p><p><span style="font-family: Courier; ">　　30&nbsp;&nbsp;&nbsp;&nbsp; {</span></p><p><span style="font-family: Courier; ">　　31&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">i = 0; i &lt; _indent; i++)&nbsp; cout &lt;&lt; "&nbsp;&nbsp;&nbsp; ";</span></p><p><span style="font-family: Courier; ">　　32&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "[" &lt;&lt; _indent &lt;&lt; "] "&lt;&lt; node.libraryName()</span></p><p><span style="font-family: Courier; ">　　33&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "::" &lt;&lt; node.className() &lt;&lt; endl;</span></p><p><span style="font-family: Courier; ">　　34</span></p><p><span style="font-family: Courier; ">　　35&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _indent++;</span></p><p><span style="font-family: Courier; ">　　36</span></p><p><span style="font-family: Courier; ">　　37&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span style="font-family: Courier; ">unsigned&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">n = 0; n &lt; node.getNumDrawables(); n++)</span></p><p><span style="font-family: Courier; ">　　38&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p><p><span style="font-family: Courier; ">　　39&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; osg::Drawable* draw = node.getDrawable(n);</span></p><p><span style="font-family: Courier; ">　　40&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!draw)</span></p><p><span style="font-family: Courier; ">　　41&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">continue</span><span style="font-family: Courier; ">;</span></p><p><span style="font-family: Courier; ">　　42&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">i = 0; i &lt;&nbsp; _indent; i++)&nbsp; cout &lt;&lt; "&nbsp;&nbsp;&nbsp; ";</span></p><p><span style="font-family: Courier; ">　　43&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "[" &lt;&lt; _indent &lt;&lt; "]" &lt;&lt; draw-&gt;libraryName() &lt;&lt; "::"</span></p><p><span style="font-family: Courier; ">　　44&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; draw-&gt;className() &lt;&lt; endl;</span></p><p><span style="font-family: Courier; ">　　45&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p><p><span style="font-family: Courier; ">　　46</span></p><p><span style="font-family: Courier; ">　　47&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; traverse(node);</span></p><p><span style="font-family: Courier; ">　　48&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; _indent--;</span></p><p><span style="font-family: Courier; ">　　49</span></p><p><span style="font-family: Courier; ">　　50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">for(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">i = 0; i &lt; _indent; i++)&nbsp; cout &lt;&lt; "&nbsp;&nbsp;&nbsp; ";</span></p><p><span style="font-family: Courier; ">　　51&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "[" &lt;&lt; _indent &lt;&lt; "]"&lt;&lt; node.libraryName()</span></p><p><span style="font-family: Courier; ">　　52&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;&lt; "::" &lt;&lt; node.className() &lt;&lt; endl;</span></p><p><span style="font-family: Courier; ">　　53&nbsp;&nbsp;&nbsp;&nbsp; }</span></p><p><span style="font-family: Courier; ">　　54&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">private</span><span style="font-family: Courier; ">:</span></p><p><span style="font-family: Courier; ">　　55&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">_indent;</span></p><p><span style="font-family: Courier; ">　　56 };</span></p><p><span style="font-family: Courier; ">　　57</span></p><p><span style="font-family: Courier; ">　　58&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">main(</span><span class="FBlue" style="color: blue; font-family: Courier; ">int&nbsp;</span><span style="font-family: Courier; ">argc, char** argv)</span></p><p><span style="font-family: Courier; ">　　59 {</span></p><p><span style="font-family: Courier; ">　　60&nbsp;&nbsp;&nbsp;&nbsp; osg::ArgumentParser&nbsp; parser(&amp;argc, argv);</span></p><p><span style="font-family: Courier; ">　　61&nbsp;&nbsp;&nbsp;&nbsp; osg::Node* root = osgDB::readNodeFiles(parser);</span></p><p><span style="font-family: Courier; ">　　62</span></p><p><span style="font-family: Courier; ">　　63&nbsp;&nbsp;&nbsp;&nbsp; if(!root)</span></p><p><span style="font-family: Courier; ">　　64&nbsp;&nbsp;&nbsp;&nbsp; {</span></p><p><span style="font-family: Courier; ">　　65&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root = osgDB::readNodeFile("avatar.osg");</span></p><p><span style="font-family: Courier; ">　　66&nbsp;&nbsp;&nbsp;&nbsp; }</span></p><p><span style="font-family: Courier; ">　　67</span></p><p><span style="font-family: Courier; ">　　68&nbsp;&nbsp;&nbsp;&nbsp; InfoVisitor infoVisitor;</span></p><p><span style="font-family: Courier; ">　　69&nbsp;&nbsp;&nbsp;&nbsp; if(root)</span></p><p><span style="font-family: Courier; ">　　70&nbsp;&nbsp;&nbsp;&nbsp; {</span></p><p><span style="font-family: Courier; ">　　71&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; root-&gt;accept(infoVisitor);</span></p><p><span style="font-family: Courier; ">　　72&nbsp;&nbsp;&nbsp;&nbsp; }</span></p><p><span style="font-family: Courier; ">　　73</span></p><p><span style="font-family: Courier; ">　　74&nbsp;&nbsp;&nbsp;&nbsp; system("pause");</span></p><p><span style="font-family: Courier; ">　　75&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="FBlue" style="color: blue; font-family: Courier; ">return</span><span style="font-family: Courier; ">&nbsp;0;</span></p><p><span style="font-family: Courier; ">　　76 }</span></p><div></div></span><img src ="http://www.cppblog.com/xx1206917580/aggbug/152515.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-08-05 10:39 <a href="http://www.cppblog.com/xx1206917580/archive/2011/08/05/152515.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>OSG开源教程(转)</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151412.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Tue, 19 Jul 2011 13:06:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151412.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/151412.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151412.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/151412.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/151412.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 整理：荣明、王伟北 京2008年4月序第一次接触OSG是在2001年，当时开源社区刚刚兴起，还没有现在这么火。下载了OSG源码，但是在看了几个Demo之后，感觉没有什么特别之处。时隔七年之后，我再次将目光投向OSG，发现OSG确实有其独到之处，很多3D效果已经不弱于甚至超过商业软件，有感于开源力量的巨大。但是，与当前主流3D商业软件如Vega、VegaPrime、VTree、Performer等相...&nbsp;&nbsp;<a href='http://www.cppblog.com/xx1206917580/archive/2011/07/19/151412.html'>阅读全文</a><img src ="http://www.cppblog.com/xx1206917580/aggbug/151412.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-19 21:06 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/19/151412.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>OSG开发概览</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151411.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Tue, 19 Jul 2011 13:01:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151411.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/151411.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/19/151411.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/151411.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/151411.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1&nbsp;OSG基础知识&#216;&nbsp;OSG是Open&nbsp;Scene&nbsp;Graphic&nbsp;的缩写，OSG于1997年诞生于以为滑翔机爱好者之手，Don&nbsp;burns&nbsp;&nbsp;为了对滑翔机的飞行进行模拟，对openGL的库进行了封装，osg的雏形就这样诞生了，1998年Don&nbsp;burns&nbsp;遇到了同样喜欢滑翔机和计算机图形...&nbsp;&nbsp;<a href='http://www.cppblog.com/xx1206917580/archive/2011/07/19/151411.html'>阅读全文</a><img src ="http://www.cppblog.com/xx1206917580/aggbug/151411.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-19 21:01 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/19/151411.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++类继承关系问题</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150481.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Fri, 08 Jul 2011 10:37:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150481.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/150481.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150481.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/150481.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/150481.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C++类继承关系问题<br />&nbsp;&nbsp;&nbsp; 在C++中继承主要有三种关系：public、protected和private。这三种继承关系中public<br />继承是最为常用的一种继承关系，代表了接口继承含义，而他们分别具体代表了什么含义呢？<br />1. public<br />&nbsp;&nbsp; 从语义角度上来说，public继承是一种接口继承，根据面向对象中的关系而言就是，子类<br />&nbsp;&nbsp; 可以代替父类完成父类接口所声明的行为，也就是必须符合&#8220;Liskov替换原则（LSP）&#8221;，<br />&nbsp;&nbsp; 此时子类可以自动转换成为父类的接口，完成接口转换。<br />&nbsp;&nbsp; 从语法角度上来说，public继承会保留父类中成员（包括函数和变量等）的可见性不变，<br />&nbsp;&nbsp; 也就是说，如果父类中的某个函数是public的，那么在被子类继承后仍然是public的。<br />&nbsp; &nbsp;<br />2. protected<br />&nbsp;&nbsp; 从语义角度上来说，protected继承是一种实现继承，根据面向对象中的关系而言就是，<br />&nbsp;&nbsp; 子类不能代替父类完成父类接口所声明的行为，也就是不符合&#8220;Liskov替换原则（LSP）&#8221;，<br />&nbsp;&nbsp; 此时子类不能自动转换成为父类的接口，就算通过类型转换（static_cast和dynamic_cast）<br />&nbsp;&nbsp; 也会得到一个空指针。<br />&nbsp;&nbsp; 从语法角度上来说，protected继承会将父类中的public可见性的成员修改成为protected<br />&nbsp;&nbsp; 可见性，相当于在子类中引入了protected成员，这样一来在子类中同样还是可以调用父<br />&nbsp;&nbsp; 类的protected和public成员，子类的子类就也可以调用被protected继承的父类的protected<br />&nbsp;&nbsp; 和public成员。<br />&nbsp;&nbsp; 例如：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample1 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void printProtected() {}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void printPublic() {}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample2 : protected CSample1 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample3 : public CSample2 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void print3() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printProtected();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printPublic();<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />3. private<br />&nbsp;&nbsp; 从语义角度上来说，private继承是一种实现继承，根据面向对象中的关系而言就是，<br />&nbsp;&nbsp; 子类不能代替父类完成父类接口所声明的行为，也就是不符合&#8220;Liskov替换原则（LSP）&#8221;，<br />&nbsp;&nbsp; 此时子类不能自动转换成为父类的接口，就算通过类型转换（static_cast和dynamic_cast）<br />&nbsp;&nbsp; 也会得到一个空指针。<br />&nbsp;&nbsp; 从语法角度上来说，private继承会将父类中的public和protected可见性的成员修改成为<br />&nbsp;&nbsp; private可见性，这样一来虽然子类中同样还是可以调用父类的protected和public成员，<br />&nbsp;&nbsp; 但是在子类的子类就不可以再调用被private继承的父类的成员了。<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample1 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; protected:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void printProtected() {}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void printPublic() {}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample2 : private CSample1 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class CSample3 : public CSample2 {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void print3() {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printProtected(); // 编译错误，不可以调用该函数<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printPublic();&nbsp;&nbsp;&nbsp; // 编译错误，不可以调用该函数<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br />&nbsp;<br />&nbsp;&nbsp;&nbsp; 在面向对象的理论中有两种概念：接口、实现，所以就出现了所谓的接口继承和实现继<br />承两种关系。而protected和private就是实现继承中所要用到的，其实protected和private<br />两者则约束继承时并没有形成两种不同的继承类别，而仅仅只是为了方便C++类方法的传递<br />调用而设计的，其实在java这样面向对象要求更为严格的语言当中，没有实现继承，他必须<br />通过委托方式来完成这一概念，如果熟悉java就会明白，如果一个对象要使用另外一个对象<br />的接口功能，而自身又不能够充当该对象所扮演的角色时，就会通过委托来完成，这样一来<br />就必须在对象中包含一个委托对象，通过对象调用语法来完成功能；在C++中就可以通过<br />protected和private继承来完成java中的委托关系（当然C++也可以形成对象委托关系），<br />那么这种情况下protected继承就容许委托可以传递（也就是被多级子类调用），而private<br />继承是不容许委托被传递的。</span><img src ="http://www.cppblog.com/xx1206917580/aggbug/150481.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-08 18:37 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/08/150481.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>经典c程序100例(31--40)</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150457.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Fri, 08 Jul 2011 05:09:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150457.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/150457.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/08/150457.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/150457.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/150457.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 【程序31】题目：请输入星期几的第一个字母来判断一下是星期几，如果第一个字母一样，则继续　　　判断第二个字母。1.程序分析：用情况语句比较好，如果第一个字母一样，则判断用情况语句或if语句判断第二个字母。2.程序源代码：【程序31】题目：请输入星期几的第一个字母来判断一下是星期几，如果第一个字母一样，则继续　　　判断第二个字母。1.程序分析：用情况语句比较好，如果第一个字母一样，则判断用情况语句或...&nbsp;&nbsp;<a href='http://www.cppblog.com/xx1206917580/archive/2011/07/08/150457.html'>阅读全文</a><img src ="http://www.cppblog.com/xx1206917580/aggbug/150457.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-08 13:09 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/08/150457.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++类模板 (非类型模板参数)</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150398.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Thu, 07 Jul 2011 09:26:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150398.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/150398.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150398.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/150398.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/150398.html</trackback:ping><description><![CDATA[<div><table style="width: 100%; table-layout: fixed"> <tbody> <tr> <td> <div id="blog_text"> <p><strong>非类型模板参数</strong></p> <p>对于函数模板和类模板，模板参数并不局限于类型，普通值也可以作为模板参数。在基于类型参数的模板中，你定义了一些具体细节未加确定的代码，直到代  码被调用时这些细节才被真正确定。然而，在这里，我们面对的这些细节是值（value），而不是类型。当要使用基于值的模板时，你必须显式地指定这些值，  才能够对模板进行实例化，并获得最终代码。在这一章里，我们将使用一个新版本的stack类模板来叙述这个特性。另外，我们还给出了一个非类型函数模板参  数的例子，并且讨论了这一技术的某些限制。</p> <p><strong>4.1&nbsp; 非类型的类模板参数</strong></p> <p>较之前一章stack例子的实现，你也可以使用元素数目固定的数组来实现stack。这个方法（用固定大小的数组）的优点是：无论是由你来亲自管理  内存，还是由标准容器来管理内存，都可以避免内存管理开销。然而，决定一个栈（stack）的最佳容量是很困难的。如果你指定的容量太小，那么栈可能会溢  出；如果指定的容量太大，那么可能会不必要地浪费内存。一个好的解决方法就是：让栈的用户亲自指定数组的大小，并把它作为所需要的栈元素的最大个数。</p> <p>为了做到这一点，你需要把数组大小定义为一个模板参数：</p> <p>//basics/stack4.hpp<br />#include &lt;stdexcept&gt;</p> <p>//template&lt;typename T = int, int MAXSIZE = 100&gt;&nbsp;&nbsp;  //可以为模板参数指定缺省值</p> <p>template &lt;typename T, int  MAXSIZE&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //由用户自己设定<br />class Stack {<br />private:<br />T  elems[MAXSIZE];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 包含元素的数组<br />int numElems;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //  元素的当前总个数</p> <p>&nbsp; public:<br />Stack();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 构造函数<br />void push(T  const&amp;);&nbsp;&nbsp;&nbsp; // 压入元素<br />void pop();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 弹出元素<br />T top()  const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 返回栈顶元素<br />bool empty() const {&nbsp;&nbsp;&nbsp; // 返回栈是否为空<br />return  numElems == 0;<br />}<br />bool full() const {&nbsp;&nbsp;&nbsp;&nbsp; // 返回栈是否已满<br />return numElems ==  MAXSIZE;<br />}<br />};</p> <p>// 构造函数<br />template &lt;typename T, int  MAXSIZE&gt;<br />Stack&lt;T,MAXSIZE&gt;::Stack  ()<br />: numElems(0)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 初始时栈不含元素<br />{<br />// 不做任何事情<br />}</p> <p>template &lt;typename T, int MAXSIZE&gt;<br />void  Stack&lt;T,MAXSIZE&gt;::push (T const&amp; elem)<br />{<br />if (numElems ==  MAXSIZE) {<br />throw std::out_of_range("Stack&lt;&gt;::push(): stack is  full");<br />}<br />elems[numElems] = elem;&nbsp;&nbsp; // 附加元素<br />++numElems;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;  // 增加元素的个数<br />}</p> <p>template&lt;typename T, int MAXSIZE&gt;<br />void  Stack&lt;T,MAXSIZE&gt;::pop ()<br />{<br />if (numElems &lt;= 0) {<br />throw  std::out_of_range("Stack&lt;&gt;::pop(): empty  stack");<br />}<br />--numElems;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 减少元素的个数<br />}</p> <p>template &lt;typename T, int MAXSIZE&gt;<br />T  Stack&lt;T,MAXSIZE&gt;::top () const<br />{<br />if (numElems &lt;= 0) {<br />throw  std::out_of_range("Stack&lt;&gt;::top(): empty stack");<br />}<br />return  elems[numElems-1];&nbsp; // 返回最后一个元素<br />}</p> <p>MAXSIZE是新加入的第2个模板参数，类型为int；它指定了数组最多可包含的栈元素的个数：<br />&nbsp;template&lt;typename T, int MAXSIZE&gt;<br />class Stack  {<br />private:<br />T elems[MAXSIZE];&nbsp;//包含元素的数组<br />...<br />};</p> <p>另外，我们使用push()来检查该栈是否已经满了：<br />template  &lt;typename T, int MAXSIZE&gt;<br />void Stack&lt;T, MAXSIZE&gt;::push (T  const&amp; elem)<br />{<br />if (numElems = = MAXSIZE ){<br />throw std::out_of_range  ("Stack&lt;&gt;::push():stack is full")<br />}<br />elems [numElems] = elem;  &nbsp;//附加元素<br />++numElems;&nbsp;&nbsp;&nbsp;&nbsp;//增加元素的个数<br />}</p> <p>为了使用这个类模板，你需要同时指定元素的类型和个数（即栈的最大容量）：<br />//basics/stack4test.cpp<br />#include &lt;iostream&gt;<br />#include  &lt;string&gt;<br />#include &lt;cstdlib&gt;<br />#include "stack4.hpp"</p> <p>int main()<br />{<br />try {<br />Stack&lt;int,20&gt;&nbsp; int20Stack;&nbsp; //  可以存储20个int元素的栈<br />Stack&lt;int,40&gt;&nbsp; int40Stack;&nbsp; //  可以存储40个int元素的栈<br />Stack&lt;std::string,40&gt; stringStack; //  可存储40个string元素的栈</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 使用可存储20个int元素的栈<br />int20Stack.push(7);<br />std::cout  &lt;&lt; int20Stack.top() &lt;&lt; std::endl;<br />int20Stack.pop();</p> <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //  使用可存储40个string的栈<br />stringStack.push("hello");<br />std::cout &lt;&lt;  stringStack.top() &lt;&lt; std::endl;  <br />stringStack.pop();<br />stringStack.pop();<br />}<br />catch (std::exception  const&amp; ex) {<br />std::cerr &lt;&lt; "Exception: " &lt;&lt; ex.what() &lt;&lt;  std::endl;<br />return EXIT_FAILURE;&nbsp; // 退出程序且有ERROR标记<br />}<br />}</p> <p>可以看出，每个模板实例都具有自己的类型，因此int20Stack和int40Stack属于不同的类型，而且这两种类型之间也不存在显式或者隐式的类型转换；所以它们之间不能互相替换，更不能互相赋值。</p> <p>同样，我们可以为模板参数指定缺省值：</p> <p>template&lt;typename T = int, int MAXSIZE =  100&gt;<br />class Stack {<br />...<br />};</p> <p>然而，如果从优化设计的观点来看，这个例子并不适合使用缺省值。缺省值应该是直观上正确的值。但是对于栈的类型和大小而言，int类型和最大容量  100从直观上看起来都不是正确的。因此，在这里最好还是让程序员显式地指定这两个值。因此我们可以在设计文档中用一条声明来说明这两个属性（即类型和最  大容量）。</p> <p>转自:http://book.51cto.com/art/200803/68250.htm</p></div></td></tr></tbody></table></div><img src ="http://www.cppblog.com/xx1206917580/aggbug/150398.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-07 17:26 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/07/150398.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++里try,catch,throw的用法</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150397.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Thu, 07 Jul 2011 08:46:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150397.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/150397.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150397.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/150397.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/150397.html</trackback:ping><description><![CDATA[<div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000; ">#include</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">iostream</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br />#include&nbsp;</span><span style="color: #000000; ">&lt;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; "><br /></span><span style="color: #0000FF; ">using</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; ">&nbsp;std;<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;Person<br />{<br /></span><span style="color: #0000FF; ">private</span><span style="color: #000000; ">:<br />&nbsp;</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;age;<br />&nbsp;</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">&nbsp;name;<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">:<br />&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;setAge(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">);<br />&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;setName(</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">);<br />};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;Error<br />{<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000FF; ">virtual</span><span style="color: #000000; ">&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;show()</span><span style="color: #000000; ">=</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;nameError:</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;Error<br />{<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">:<br />&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;show()<br />&nbsp;{<br />&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">name&nbsp;is&nbsp;error</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;}<br />&nbsp;<br />};<br /></span><span style="color: #0000FF; ">class</span><span style="color: #000000; ">&nbsp;ageError:</span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">&nbsp;Error<br />{<br /></span><span style="color: #0000FF; ">public</span><span style="color: #000000; ">:<br />&nbsp;</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;show()<br />&nbsp;{<br />&nbsp;&nbsp;&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">age&nbsp;is&nbsp;error</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;}<br />};<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;Person::setAge(</span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;a)<br />{<br />&nbsp;ageError&nbsp;ag;<br />&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(a</span><span style="color: #000000; ">&lt;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">||</span><span style="color: #000000; ">a</span><span style="color: #000000; ">&gt;</span><span style="color: #000000; ">100</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">throw</span><span style="color: #000000; ">&nbsp;ag;<br />&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">age</span><span style="color: #000000; ">=</span><span style="color: #000000; ">a;<br />}<br /></span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">&nbsp;Person::setName(</span><span style="color: #0000FF; ">string</span><span style="color: #000000; ">&nbsp;str)<br />{<br />&nbsp;nameError&nbsp;ne;<br />&nbsp;</span><span style="color: #0000FF; ">if</span><span style="color: #000000; ">(str</span><span style="color: #000000; ">==</span><span style="color: #000000; ">"</span><span style="color: #000000; ">exit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">)<br />&nbsp;&nbsp;</span><span style="color: #0000FF; ">throw</span><span style="color: #000000; ">&nbsp;ne;<br />&nbsp;</span><span style="color: #0000FF; ">this</span><span style="color: #000000; ">-&gt;</span><span style="color: #000000; ">name</span><span style="color: #000000; ">=</span><span style="color: #000000; ">str;<br />}<br /><br /></span><span style="color: #0000FF; ">int</span><span style="color: #000000; ">&nbsp;main(</span><span style="color: #0000FF; ">void</span><span style="color: #000000; ">)<br />{<br />&nbsp;&nbsp;Person&nbsp;p;<br />&nbsp;</span><span style="color: #0000FF; ">try</span><span style="color: #000000; "><br />&nbsp;{<br />&nbsp;&nbsp;p.setAge(</span><span style="color: #000000; ">0</span><span style="color: #000000; ">);<br />&nbsp;&nbsp;p.setName(</span><span style="color: #000000; ">"</span><span style="color: #000000; ">exit</span><span style="color: #000000; ">"</span><span style="color: #000000; ">);<br />&nbsp;}<br />&nbsp;</span><span style="color: #0000FF; ">catch</span><span style="color: #000000; ">(Error&nbsp;</span><span style="color: #000000; ">&amp;</span><span style="color: #000000; ">er)<br />&nbsp;{<br />&nbsp;&nbsp;&nbsp;er.show();<br />&nbsp;}<br />&nbsp;cout</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">"</span><span style="color: #000000; ">hello&nbsp;world</span><span style="color: #000000; ">"</span><span style="color: #000000; ">&lt;&lt;</span><span style="color: #000000; ">endl;<br />&nbsp;</span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">&nbsp;</span><span style="color: #000000; ">0</span><span style="color: #000000; ">;<br />}</span></div><img src ="http://www.cppblog.com/xx1206917580/aggbug/150397.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-07 16:46 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/07/150397.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++模板使用介绍</title><link>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150394.html</link><dc:creator>Smart Pointer</dc:creator><author>Smart Pointer</author><pubDate>Thu, 07 Jul 2011 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150394.html</guid><wfw:comment>http://www.cppblog.com/xx1206917580/comments/150394.html</wfw:comment><comments>http://www.cppblog.com/xx1206917580/archive/2011/07/07/150394.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xx1206917580/comments/commentRss/150394.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xx1206917580/services/trackbacks/150394.html</trackback:ping><description><![CDATA[<div><span style="line-height: 22px; font-family: arial, sans-serif, 宋体; "><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-indent: -18pt; margin-left: 18pt; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; font-size: 14pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; ">1.&nbsp;</span></span></strong><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; font-size: 14pt; ">模板的概念。</span></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">我们已经学过<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">重载<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">(Overloading)</span></span></strong>，对重载函数而言<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">,C++</span>的检查机制能通过函数参数的不同及所属类的不同。正确的调用重载函数。例如，为<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">求两个数的最大值</span>，我们定义<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">MAX()</span>函数需要对不同的数据类型分别定义不同<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">重载<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">(Overload)</span></span></strong>版本。</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">函数<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">1.</span></span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">int max(<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">int</span>&nbsp;x,<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">int</span>&nbsp;y);<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />{return(x&gt;y)?x:y ;}</span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">函数<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">2.</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />float max(&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">float</span>&nbsp;x,<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">float</span>&nbsp;y){<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />return (x&gt;y)? x:y ;}</span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">函数<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">3.</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />double max(<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">double</span>&nbsp;x,<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">double</span>&nbsp;y)<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />{return (c&gt;y)? x:y ;}</span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">但如果在主函数中，我们分别定义了<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;char a,b;&nbsp;</span>那么在执行<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">max(a,b);</span>时 程序就会出错，因为我们没有定义<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">char</span>类型的重载版本。</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">现在，我们再重新审视上述的<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">max()</span>函数，它们都具有同样的功能，即求两个数的最大值，能否只写一套代码解决这个问题呢？这样就会避免因重载函数定义不 全面而带来的调用错误。为解决上述问题<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">C++</span>引入模板机制，<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">模板定义：模板就是实现代码重用机制的一种工具，它可以实现类型参数化，即把类型定义为参数， 从而实现了真正的代码可重用性。模版可以分为两类，一个是函数模版，另外一个是类模版。</span></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-indent: -18pt; margin-left: 18pt; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">2.<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;&nbsp;&nbsp;</span></span></span></strong><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; font-size: 14pt; ">函数模板的写法</span></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">函数模板的一般形式如下：</p><p style="font-family: arial, sans-serif, 宋体; line-height: 13pt; "><em style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">Template &lt;<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">class</strong></span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">或者也可以用<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">typename</span></strong><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;T&gt;</span></span></em></p><p style="font-family: arial, sans-serif, 宋体; line-height: 13pt; "><em style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">返回类型 函数名（形参表）<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />{//</span>函数定义体<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;}</span></span></em></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">说明：<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;template</span>是一个声明模板的关键字，表示声明一个模板关键字<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">class</span>不能省略，如果类型形参多余一个 ，每个形参前都要加<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">class &lt;</span>类型 形参表<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&gt;</span>可以包含基本数据类型可以包含类类型<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">.</span></span></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">请看以下程序<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">:</span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">//Test.cpp</span></strong></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#include</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">&lt;iostream&gt;</span></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">using</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;std::cout;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">using</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;std::endl;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">声明一个函数模版<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">,</span>用来比较输入的两个相同数据类型的参数的大小，<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">class</span>也可以被<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">typename</span>代替，</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">//T</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">可以被任何字母或者数字代替。</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">template</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">class</span>&nbsp;T&gt;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">T min(T x,T y)</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">{&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">return</span>(x&lt;y)?x:y;}</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">void</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;main( )</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>&nbsp;n1=2,n2=10;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">double</span>&nbsp;d1=1.5,d2=5.6;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>cout&lt;&lt;&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; font-size: 9pt; ">较小整数<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">:"</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&lt;&lt;min(n1,n2)&lt;&lt;endl;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>cout&lt;&lt;&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; font-size: 9pt; ">较小实数<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">:"</span></span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&lt;&lt;min(d1,d2)&lt;&lt;endl;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>system(<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"PAUSE"</span>);</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">}</span></p><p align="center" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: center; ">程序运行结果：　</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">程序分析：<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">main()</span>函数中定义了两个整型变量<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">n1 , n2&nbsp;</span>两个双精度类型变量<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">d1 , d2</span>然后调用<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">min( n1, n2);&nbsp;</span>即实例化函数模板<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">T min(T x, T y)</span>其中Ｔ为<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">int</span>型，求出<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">n1,n2</span>中的最小值．同理调用<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">min(d1,d2)</span>时，求出<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">d1,d2</span>中的最小值．</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-indent: -18pt; margin-left: 18pt; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; font-size: 14pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; ">3.&nbsp;</span></span></strong><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 31px; font-size: 14pt; ">类模板的写法</span></strong></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">定义一个类模板：</p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><em style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">Template &lt;<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;class</strong></span><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: green; ">或者也可以用<strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">typename</span></strong><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;T &gt;<br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " />class</span>类名｛<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /></span>／／类定义．．．．．．<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /></span>｝；</span></em></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; color: red; ">说明：其中，<span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">template</span>是声明各模板的关键字，表示声明一个模板，模板参数可以是一个，也可以是多个。</span></strong></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">例如：定义一个类模板：</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 26px; font-size: 12pt; ">// ClassTemplate.h</span></strong><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /></span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#ifndef</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;ClassTemplate_HH</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#define</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;ClassTemplate_HH</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">template</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T1,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T2&gt;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">class</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;myClass{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">private</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">:</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>T1 I;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>T2 J;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">public</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">:</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>myClass(T1 a, T2 b);<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; ">//Constructor</span></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">void</span>&nbsp;show();</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">};</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">这是构造函数</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">注意这些格式</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">template</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T1,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T2&gt;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">myClass&lt;T1,T2&gt;::myClass(T1 a,T2 b):I(a),J(b){}</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">//</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: green; font-size: 9pt; ">这是<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">void show();</span></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">template</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T1,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T2&gt;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">void</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;myClass&lt;T1,T2&gt;::show()</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>cout&lt;&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"I="</span>&lt;&lt;I&lt;&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">", J="</span>&lt;&lt;J&lt;&lt;endl;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">}</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#endif</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 26px; font-size: 12pt; ">// Test.cpp</span></strong></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#include</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">&lt;iostream&gt;</span></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">#include</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"ClassTemplate.h"</span></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">using</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;std::cout;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">using</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;std::endl;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">void</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;main()</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>myClass&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>&gt; class1(3,5);</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>class1.show();</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>myClass&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">char</span>&gt; class2(3,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">'a'</span>);</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>class2.show();</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>myClass&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">double</span>,<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>&gt; class3(2.9,10);</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>class3.show();</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>system(<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: #a31515; ">"PAUSE"</span>);</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">}</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 26px; font-size: 12pt; ">最后结果显示：</span></p><p align="center" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: center; "></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;</p><div align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><strong style="font-family: arial, sans-serif, 宋体; line-height: 29px; ">4.非类型模版参数</strong><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">一般来说，非类型模板参数可以是常整数（包括枚举）或者指向外部链接对象的指针。</span></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">那么就是说，浮点数是不行的，指向内部链接对象的指针是不行的。</span></p><br style="font-family: arial, sans-serif, 宋体; line-height: 22px; " /><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">template</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">typename</span>&nbsp;T,&nbsp;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>&nbsp;MAXSIZE&gt;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; font-size: 9pt; ">class</span><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&nbsp;Stack{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">Private:</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>T elems[MAXSIZE];</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&#8230;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">};</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "></span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">Int main()</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">{</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Stack&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>, 20&gt; int20Stack;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>Stack&lt;<span style="font-family: arial, sans-serif, 宋体; line-height: 19px; color: blue; ">int</span>, 40&gt; int40Stack;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">&#8230;</span></p><p align="left" style="font-family: arial, sans-serif, 宋体; line-height: 22px; text-align: left; "></p><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><span style="font-family: arial, sans-serif, 宋体; line-height: 19px; font-size: 9pt; ">};</span></p></div><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;</p><div forimg="1" align="center" style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><img border="0" alt="" small="0" src="http://www.kuqin.com/upimg/allimg/090405/0120370.jpg" style="font-family: arial, sans-serif, 宋体; line-height: 22px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></div><p style="font-family: arial, sans-serif, 宋体; line-height: 22px; ">&nbsp;</p><div forimg="1" align="center" style="font-family: arial, sans-serif, 宋体; line-height: 22px; "><img border="0" alt="" small="0" src="http://www.kuqin.com/upimg/allimg/090405/0120371.jpg" style="font-family: arial, sans-serif, 宋体; line-height: 22px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></div></span></div><img src ="http://www.cppblog.com/xx1206917580/aggbug/150394.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xx1206917580/" target="_blank">Smart Pointer</a> 2011-07-07 16:12 <a href="http://www.cppblog.com/xx1206917580/archive/2011/07/07/150394.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>