﻿<?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++博客-godson</title><link>http://www.cppblog.com/godson/</link><description /><language>zh-cn</language><lastBuildDate>Sat, 18 Apr 2026 06:46:44 GMT</lastBuildDate><pubDate>Sat, 18 Apr 2026 06:46:44 GMT</pubDate><ttl>60</ttl><item><title>模板的定义与实现分离</title><link>http://www.cppblog.com/godson/archive/2009/10/26/99478.html</link><dc:creator>伴我闯天涯</dc:creator><author>伴我闯天涯</author><pubDate>Mon, 26 Oct 2009 05:48:00 GMT</pubDate><guid>http://www.cppblog.com/godson/archive/2009/10/26/99478.html</guid><description><![CDATA[模板定义和实现分离：<br><br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#ifndef&nbsp;_TEST_H_<br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">#define</span><span style="color: #000000;">&nbsp;_TEST_H_</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">template&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;test&nbsp;{<br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;test(T&nbsp;v1,&nbsp;T&nbsp;v2);<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;test();<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;compare()&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">private</span><span style="color: #000000;">:<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;_v1;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;T&nbsp;_v2;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">};<br></span><span style="color: #008080;">15</span> <span style="color: #000000;">//此处包含test.cpp实现分离<br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">test.cpp</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;">17</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">18</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">#endif</span><span style="color: #000000;"><br></span><span style="color: #008080;">19</span>&nbsp;<span style="color: #000000;"></span></div>
<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">template</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">test</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::test()&nbsp;:&nbsp;_v1(</span><span style="color: #000000;">0</span><span style="color: #000000;">),_v2(</span><span style="color: #000000;">1</span><span style="color: #000000;">)&nbsp;{<br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;">template</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">test</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::test(T&nbsp;v1,&nbsp;T&nbsp;v2):&nbsp;_v1(v1),_v2(v2)&nbsp;{<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">template</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;test</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::compare()&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;{<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;_v1</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">_v2</span><span style="color: #000000;">?</span><span style="color: #000000;">1</span><span style="color: #000000;">:</span><span style="color: #000000;">-</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"></span></div>
<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">test.h</span><span style="color: #000000;">"</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></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: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()&nbsp;{<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">输入两个数：</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;value1,value2;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cin&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;value1&nbsp;</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">&nbsp;value2;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;test</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">&nbsp;t(value1,value2);<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;t.compare()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">endl;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br></span><span style="color: #008080;">14</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">15</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">16</span>&nbsp;<span style="color: #000000;"></span></div>
g++ main.cpp -o main编译通过<br>这个可以作用于函数模板和类模板<br><br>C++标准有一个export关键字用来实现模板的定义和实现分离的，只是到现在没有找到支持这个关键字的编译器，有点扯淡。<br><br> <img src ="http://www.cppblog.com/godson/aggbug/99478.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/godson/" target="_blank">伴我闯天涯</a> 2009-10-26 13:48 <a href="http://www.cppblog.com/godson/archive/2009/10/26/99478.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>单向链表的学习</title><link>http://www.cppblog.com/godson/archive/2009/10/11/98319.html</link><dc:creator>伴我闯天涯</dc:creator><author>伴我闯天涯</author><pubDate>Sun, 11 Oct 2009 10:46:00 GMT</pubDate><guid>http://www.cppblog.com/godson/archive/2009/10/11/98319.html</guid><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;现在正在看数据结构，感觉清华大学出版的《数据结构与C++高级教程(第3版)》里翻译有很多逻辑上的错误，虽然如此，但书上还是有很多不错的地方的。今天看到链表，算是复习一下，先把代码帖出来，慢慢再总结：listitem.h:CodeCode highlighting produced by Actipro CodeHi...&nbsp;&nbsp;<a href='http://www.cppblog.com/godson/archive/2009/10/11/98319.html'>阅读全文</a><img src ="http://www.cppblog.com/godson/aggbug/98319.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/godson/" target="_blank">伴我闯天涯</a> 2009-10-11 18:46 <a href="http://www.cppblog.com/godson/archive/2009/10/11/98319.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>