锘??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美多人爱爱视频网站,国产欧美一区二区精品性,欧美激情精品久久久http://m.shnenglu.com/ant/The dreams in which I'm dying are the best I've ever had...zh-cnSat, 15 Nov 2025 23:11:20 GMTSat, 15 Nov 2025 23:11:20 GMT60strlen婧愮爜鍓栨瀽http://m.shnenglu.com/ant/archive/2007/10/12/32886.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 12 Oct 2007 05:19:00 GMThttp://m.shnenglu.com/ant/archive/2007/10/12/32886.htmlhttp://m.shnenglu.com/ant/comments/32886.htmlhttp://m.shnenglu.com/ant/archive/2007/10/12/32886.html#Feedback34http://m.shnenglu.com/ant/comments/commentRss/32886.htmlhttp://m.shnenglu.com/ant/services/trackbacks/32886.html闃呰鍏ㄦ枃

]]>
TEA鍔犲瘑綆楁硶鐨凜/C++瀹炵幇http://m.shnenglu.com/ant/archive/2007/10/12/31326.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 12 Oct 2007 05:17:00 GMThttp://m.shnenglu.com/ant/archive/2007/10/12/31326.htmlhttp://m.shnenglu.com/ant/comments/31326.htmlhttp://m.shnenglu.com/ant/archive/2007/10/12/31326.html#Feedback14http://m.shnenglu.com/ant/comments/commentRss/31326.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31326.html闃呰鍏ㄦ枃

]]>
Win32 Service鐨勭畝鍗曞皝瑁?/title><link>http://m.shnenglu.com/ant/archive/2007/09/24/32769.html</link><dc:creator>铓傝殎緇堢粨鑰?/dc:creator><author>铓傝殎緇堢粨鑰?/author><pubDate>Mon, 24 Sep 2007 05:01:00 GMT</pubDate><guid>http://m.shnenglu.com/ant/archive/2007/09/24/32769.html</guid><wfw:comment>http://m.shnenglu.com/ant/comments/32769.html</wfw:comment><comments>http://m.shnenglu.com/ant/archive/2007/09/24/32769.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://m.shnenglu.com/ant/comments/commentRss/32769.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/ant/services/trackbacks/32769.html</trackback:ping><description><![CDATA[<p>鍓嶆鏃墮棿瀛︿範(fàn)Windows紼嬪簭璁捐錛屽垰濂藉鍒癢in32 Service錛屼簬鏄啓浜嗕袱涓畝鍗曠殑綾伙細(xì)BaseService鍜孲erviceCtrl銆傝櫧鐒跺姛鑳芥瘮杈冪畝鍗曪紝浣嗘槸涔熻兘閫傜敤浜庡ぇ澶氭暟鎯呭喌銆備笅闈粙緇嶄竴涓嬬畝鍗曠敤娉曪紝濡傛灉浣犲垰濂介渶瑕佸啓涓浜涚畝鍗曠殑鏈嶅姟紼嬪簭錛岃繖涓や釜綾諱篃璁歌兘媧句笂鐢ㄥ満錛?br><br><span style="FONT-SIZE: 14pt"><strong style="COLOR: #993366">1. BaseService<br></strong></span><em style="COLOR: #333399">BaseService.h<br></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef BASE_SERVICE_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define BASE_SERVICE_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span></font> <font face="Courier New"><span style="COLOR: #000000"> BaseService {<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 6</span> </font> <font face="Courier New"><span style="COLOR: #000000">    explicit BaseService(LPCTSTR szServiceName,<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">                DWORD dwServiceType </span><span style="COLOR: #000000">=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_WIN32_OWN_PROCESS,<br></span><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">                DWORD dwStartType </span><span style="COLOR: #000000">=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_AUTO_START);<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #000000">~</span></font> <font face="Courier New"><span style="COLOR: #000000">BaseService() {}<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    bool ParseStandardArgs(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"><span style="COLOR: #000000"> argv[]);<br></span><span style="COLOR: #008080">11</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool IsInstalled();<br></span><span style="COLOR: #008080">12</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Install();<br></span><span style="COLOR: #008080">13</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Uninstall();<br></span><span style="COLOR: #008080">14</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Start();<br></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> Run() </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">    virtual bool OnInitialize() { </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnStop() {}<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnPause() {}<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnContinue() {}<br></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnInterrogate() {}<br></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnShutdown() {}<br></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> OnUserControl(DWORD dwControl) {}<br></span><span style="COLOR: #008080">24</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ...<br></span><span style="COLOR: #008080">25</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">26</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">BASE_SERVICE_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>瑕佸疄鐜拌嚜宸辯殑鏈嶅姟綾誨彧闇浠嶣aseService緇ф壙騫朵笖Override鐩稿叧鐨剉irtual鍑芥暟鍗沖彲錛屼笅闈㈢ず鑼冧竴涓狟eepService綾伙紝璇ユ湇鍔″彧鏄畝鍗曞湴姣忛殧2縐抌eep涓涓嬶紝涓轟簡綆鍗曟墍鏈変唬鐮佸潎鏀懼湪.h鏂囦歡涓細(xì)<br><em style="COLOR: #333399">BeepService.h</em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef BEEP_SERVICE_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define BEEP_SERVICE_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BaseService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000"> BeepService : </span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000"> BaseService {<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 8</span> </font> <font face="Courier New"><span style="COLOR: #000000">    BeepService(LPCTSTR szServiceName)<br></span><span style="COLOR: #008080"> 9</span> </font> <font face="Courier New"><span style="COLOR: #000000">        :BaseService(szServiceName)<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">        ,m_bPaused(</span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">)<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ,m_bRunning(</span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">) {}<br></span><span style="COLOR: #008080">12</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnStop() { m_bRunning </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnPause() { m_bPaused </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> OnContinue() { m_bPaused </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">false</span></font> <font face="Courier New"><span style="COLOR: #000000">; }<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">    virtual </span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"><span style="COLOR: #000000"> Run() {<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">        m_bRunning </span><span style="COLOR: #000000">=</span><span style="COLOR: #000000"> </span><span style="COLOR: #0000ff">true</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">while</span></font> <font face="Courier New"><span style="COLOR: #000000"> (m_bRunning) {<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">m_bPaused)<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">                Beep(</span><span style="COLOR: #000000">800</span><span style="COLOR: #000000">, </span><span style="COLOR: #000000">800</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">21</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">2000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">22</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">23</span> </font> <font face="Courier New"><span style="COLOR: #000000">    }<br></span><span style="COLOR: #008080">24</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">private</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">volatile</span></font> <font face="Courier New"><span style="COLOR: #000000"> bool m_bPaused;<br></span><span style="COLOR: #008080">26</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">volatile</span></font> <font face="Courier New"><span style="COLOR: #000000"> bool m_bRunning;<br></span><span style="COLOR: #008080">27</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">28</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">29</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">BEEP_SERVICE_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>閫氬父鏉ヨ鍙』瑕丱verride涓婇潰鐨?涓獀irtual鍑芥暟灝監(jiān)K浜嗭細(xì)<br>鍦?em>Run()</em>涓繘琛屽疄闄呯殑宸ヤ綔錛?em>OnStop(),OnPause(),OnContinue()</em>鍒欐槸涓轟簡鍝嶅簲Service Control Manager鐨勬帶鍒躲?br><em style="COLOR: #333399"><br>test.cpp<br></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">tchar.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span></font> <font face="Courier New"><span style="COLOR: #000000">argv[]) {<br></span><span style="COLOR: #008080"> 7</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000">    BeepService beepService(_T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080"> 9</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">beepService.ParseStandardArgs(argc, argv)) {<br></span><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span></font> <font face="Courier New"><span style="COLOR: #000000"> (beepService.IsInstalled()) {<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">            </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (</span><span style="COLOR: #000000">!</span></font> <font face="Courier New"><span style="COLOR: #000000">beepService.Start())<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">                printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The service can not run from command line.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">        } </span><span style="COLOR: #0000ff">else</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">The service is not installed, </span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">15</span> <span style="COLOR: #000000">                </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">use \</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%</span><span style="COLOR: #000000">s </span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">i\</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"> to install.\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">, argv[</span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">]);<br></span><span style="COLOR: #008080">16</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">17</span> </font> <font face="Courier New"><span style="COLOR: #000000">    }<br></span><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">}</span></font> </div> <p>鍋囪緙栬瘧鍚庣敓鎴愮殑exe鏂囦歡涓篵eep.exe錛屽垯鍦ㄥ懡浠よ涓彲浠ュ涓嬩嬌鐢細(xì)<br><em style="COLOR: #333399">(1). beep -i    瀹夎service(瀹夎浠ュ悗緋葷粺榪愯鏃朵細(xì)鑷姩鍚姩)<br>(2). beep -u   鍗歌澆service(濡傛灉service姝e湪榪愯錛屽垯鍏堝仠姝ervice鍐嶅嵏杞?<br></em>BaseServiced 鐨凱arseStandardArgs姝f槸鐢ㄦ潵瑙f瀽涓婅堪涓や釜鍛戒護(hù)銆?br><br><span style="FONT-SIZE: 14pt"><strong style="COLOR: #993366">2. ServiceCtrl<br></strong></span>铏界劧Windows鑷甫鐨凷ervice Control Manager鍙互鎺у埗鏈嶅姟紼嬪簭錛屼絾鏄緢澶氭椂鍊欐垜浠兘闇瑕佺敤浠g爜鎺у埗錛岃繖灝辯敤鍒癝erviceCtrl綾伙紝璇ョ被鐨勬帴鍙e涓嬶細(xì)<br><em style="COLOR: #333399">ServiceCtrl.h</em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> </font> <font face="Courier New"><span style="COLOR: #000000">#ifndef SERVICE_CTRL_H<br></span><span style="COLOR: #008080"> 2</span> </font> <font face="Courier New"><span style="COLOR: #000000">#define SERVICE_CTRL_H<br></span><span style="COLOR: #008080"> 3</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">class</span></font> <font face="Courier New"><span style="COLOR: #000000"> ServiceCtrl {<br></span><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">public</span></font> <font face="Courier New"><span style="COLOR: #000000">:<br></span><span style="COLOR: #008080"> 6</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ServiceCtrl(LPCTSTR szServiceName);<br></span><span style="COLOR: #008080"> 7</span> <span style="COLOR: #000000">    </span><span style="COLOR: #000000">~</span></font> <font face="Courier New"><span style="COLOR: #000000">ServiceCtrl();<br></span><span style="COLOR: #008080"> 8</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Start();<br></span><span style="COLOR: #008080"> 9</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Pause();<br></span><span style="COLOR: #008080">10</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Continue();<br></span><span style="COLOR: #008080">11</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Stop();<br></span><span style="COLOR: #008080">12</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool Interrogate();<br></span><span style="COLOR: #008080">13</span> </font> <font face="Courier New"><span style="COLOR: #000000">    bool UserControl(DWORD dwControl);<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">    DWORD State() </span><span style="COLOR: #0000ff">const</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">15</span> </font> <font face="Courier New"><span style="COLOR: #000000">    ...<br></span><span style="COLOR: #008080">16</span> </font> <font face="Courier New"><span style="COLOR: #000000">};<br></span><span style="COLOR: #008080">17</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">18</span> <span style="COLOR: #000000">#endif</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">SERVICE_CTRL_H</span><span style="COLOR: #008000">*/</span></font> </div> <p>鎺ュ彛姣旇緝鐩磋娌′粈涔堝ソ璇寸殑錛岀湅涓嬮潰鐨勭ず渚嬩唬鐮侊細(xì)<br><em><font color=#333399>test.cpp</font></em></p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008080"> 1</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">windows.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 2</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">tchar.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 3</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">stdio.h</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 4</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">exception</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 5</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 6</span> <span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">ServiceCtrl.h</span><span style="COLOR: #000000">"</span></font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 7</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080"> 8</span> <span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> main(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000"> argc, </span><span style="COLOR: #0000ff">char</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">*</span></font> <font face="Courier New"><span style="COLOR: #000000">argv[]) {<br></span><span style="COLOR: #008080"> 9</span> </font> <span style="COLOR: #000000"><br></span><font face="Courier New"><span style="COLOR: #008080">10</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">try</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">11</span> <span style="COLOR: #000000">        ServiceCtrl servCtrl(_T(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">BeepService</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">));<br></span><span style="COLOR: #008080">12</span> <span style="COLOR: #000000">        </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000"> (servCtrl.State() </span><span style="COLOR: #000000">!=</span></font> <font face="Courier New"><span style="COLOR: #000000"> SERVICE_STOPPED) {<br></span><span style="COLOR: #008080">13</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Service already started.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">14</span> <span style="COLOR: #000000">        } </span><span style="COLOR: #0000ff">else</span></font> <font face="Courier New"><span style="COLOR: #000000"> {<br></span><span style="COLOR: #008080">15</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Start();<br></span><span style="COLOR: #008080">16</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Start.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">17</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">18</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Pause();<br></span><span style="COLOR: #008080">19</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Pause.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">20</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">21</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Continue();<br></span><span style="COLOR: #008080">22</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Continue.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">23</span> <span style="COLOR: #000000">            Sleep(</span><span style="COLOR: #000000">6000</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">24</span> </font> <font face="Courier New"><span style="COLOR: #000000">            servCtrl.Stop();<br></span><span style="COLOR: #008080">25</span> <span style="COLOR: #000000">            printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">Stop.\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">);<br></span><span style="COLOR: #008080">26</span> </font> <font face="Courier New"><span style="COLOR: #000000">        }<br></span><span style="COLOR: #008080">27</span> <span style="COLOR: #000000">    } </span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000"> (std::exception </span><span style="COLOR: #000000">&</span></font> <font face="Courier New"><span style="COLOR: #000000">e) {<br></span><span style="COLOR: #008080">28</span> <span style="COLOR: #000000">        printf(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">%s\n</span><span style="COLOR: #000000">"</span></font> <font face="Courier New"><span style="COLOR: #000000">, e.what());<br></span><span style="COLOR: #008080">29</span> </font> <font face="Courier New"><span style="COLOR: #000000">    }<br></span><span style="COLOR: #008080">30</span> <span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000"> </span><span style="COLOR: #000000">0</span></font> <font face="Courier New"><span style="COLOR: #000000">;<br></span><span style="COLOR: #008080">31</span> <span style="COLOR: #000000">}</span></font> </div> <br>婧愪唬鐮侊細(xì)<a title=鐐瑰嚮涓嬭澆 href="http://m.shnenglu.com/Files/ant/Service.rar">鐐瑰嚮涓嬭澆</a><br> <img src ="http://m.shnenglu.com/ant/aggbug/32769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/ant/" target="_blank">铓傝殎緇堢粨鑰?/a> 2007-09-24 13:01 <a href="http://m.shnenglu.com/ant/archive/2007/09/24/32769.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>MD5綆楁硶鐨凜++瀹炵幇http://m.shnenglu.com/ant/archive/2007/09/11/31886.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Tue, 11 Sep 2007 04:20:00 GMThttp://m.shnenglu.com/ant/archive/2007/09/11/31886.htmlhttp://m.shnenglu.com/ant/comments/31886.htmlhttp://m.shnenglu.com/ant/archive/2007/09/11/31886.html#Feedback121http://m.shnenglu.com/ant/comments/commentRss/31886.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31886.html闃呰鍏ㄦ枃

]]>
闈炲畬緹嶤++ Singleton瀹炵幇[2]http://m.shnenglu.com/ant/archive/2007/09/07/31786.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 07 Sep 2007 15:22:00 GMThttp://m.shnenglu.com/ant/archive/2007/09/07/31786.htmlhttp://m.shnenglu.com/ant/comments/31786.htmlhttp://m.shnenglu.com/ant/archive/2007/09/07/31786.html#Feedback13http://m.shnenglu.com/ant/comments/commentRss/31786.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31786.html4.瑙e喅澶氱嚎紼嬮棶棰?br />涓婁竴綃囧疄鐜扮殑Singleton鍙兘鍦ㄥ崟綰跨▼鐜涓嬌鐢紝鍦ㄥ綰跨▼鐜涓細(xì)鍑虹幇寰堝闂錛岀湅Instance()瀹炵幇浠g爜錛?br />
1static聽Singleton&聽Instance()聽{
2聽聽聽聽if聽(0==聽_instance)聽{聽//1

3聽聽聽聽聽聽聽聽_instance聽=new聽Singleton();聽//2
4聽聽聽聽聽聽聽聽atexit(Destroy);
5
聽聽聽聽}
6聽聽聽聽return*_instance;聽//3

7}
鑰冭檻濡備笅鎯呭喌錛氱嚎紼嬩竴璋冪敤Instance()錛岃繘鍏?/1錛? == _instance 榪斿洖true錛岀嚎紼嬩竴浜庢槸榪涘叆//2銆傝繖鏃跺欑嚎紼嬩竴琚寕璧鳳紝綰跨▼浜屽紑濮嬫墽琛岋紝綰跨▼浜岃皟鐢↖nstance()錛岃繘鍏?/1錛屽彂鐜? == _instance 浠嶇劧榪斿洖true錛岀嚎紼嬩簩浜庢槸涔熻繘鍏?/2錛岀嚎紼嬩簩緇х畫鎵ц鍒?/3鐩村埌榪斿洖銆傝繖鏃跺欑嚎紼嬩竴琚敜閱掞紝緇х畫浠?/2寮濮嬫墽琛岋紝榪欏皢浼?xì)瑕嗙洊绾拷E嬩簩鍒涘緩鐨刜instance錛岀嚎紼嬩竴緇х畫鎵ц鍒?/3鐩村埌榪斿洖...

瑙e喅鏂規(guī)硶寰堢畝鍗曪紝寮曞叆鐩稿叧鍚屾瀵硅薄(synchronization object)灝辮浜嗭紝渚嬪鍦╳in32騫沖彴涓嬪彲浠ュ涓嬪疄鐜幫細(xì)
synobj.h
聽1#ifndef聽SYNOBJ_H
聽2
#define聽SYNOBJ_H
聽3

聽4#include聽<windows.h>
聽5
聽6#define聽CLASS_UNCOPYABLE(classname)聽\
聽7聽聽聽聽private
:聽\
聽8聽聽聽聽classname(const聽classname&
);聽\
聽9聽聽聽聽classname&聽operator=(const聽classname&
);
10

11class聽Mutex聽{
12
聽聽聽聽CLASS_UNCOPYABLE(Mutex)
13public
:
14聽聽聽聽Mutex()聽:_cs()聽{聽InitializeCriticalSection(&
_cs);聽}
15聽聽聽聽~Mutex()聽{聽DeleteCriticalSection(&
_cs);聽}
16聽聽聽聽void聽lock()聽{聽EnterCriticalSection(&
_cs);聽}
17聽聽聽聽void聽unlock()聽{聽LeaveCriticalSection(&
_cs);聽}
18private
:
19
聽聽聽聽CRITICAL_SECTION聽_cs;
20
};
21

22class聽Lock聽{
23
聽聽聽聽CLASS_UNCOPYABLE(Lock)
24public
:
25聽聽聽聽explicit聽Lock(Mutex&
聽cs)聽:_cs(cs)聽{聽_cs.lock();聽}
26聽聽聽聽~
Lock()聽{聽_cs.unlock();聽}
27private
:
28聽聽聽聽Mutex&
聽_cs;
29
};
30

31#endif/*SYNOBJ_H*/

鏈変簡鍚屾瀵硅薄寰堝鏄撳氨鑳藉鍐欏嚭濡備笅浠g爜錛?br />singleton.h
聽1#ifndef聽SINGLETON_H
聽2
#define聽SINGLETON_H
聽3

聽4#include聽"synobj.h"
聽5
聽6class聽Singleton聽{
聽7public
:
聽8聽聽聽聽static聽Singleton&聽Instance()聽{聽//聽Unique聽point聽of聽access

聽9聽聽聽聽聽聽聽聽Lock聽lock(_mutex);
10聽聽聽聽聽聽聽聽if聽(0==
聽_instance)聽{
11聽聽聽聽聽聽聽聽聽聽聽聽_instance聽=new
聽Singleton();
12聽聽聽聽聽聽聽聽聽聽聽聽atexit(Destroy);聽//聽Register聽Destroy聽function

13聽聽聽聽聽聽聽聽}
14聽聽聽聽聽聽聽聽return*
_instance;
15
聽聽聽聽}
16聽聽聽聽void
聽DoSomething(){}
17private
:
18聽聽聽聽staticvoid聽Destroy()聽{聽//聽Destroy聽the聽only聽instance

19聽聽聽聽聽聽聽聽if聽(聽_instance聽!=0聽)聽{
20
聽聽聽聽聽聽聽聽聽聽聽聽delete聽_instance;
21聽聽聽聽聽聽聽聽聽聽聽聽_instance聽=0
;
22
聽聽聽聽聽聽聽聽}
23
聽聽聽聽}
24聽聽聽聽Singleton(){}聽//聽Prevent聽clients聽from聽creating聽a聽new聽Singleton

25聽聽聽聽~Singleton(){}聽//聽Prevent聽clients聽from聽deleting聽a聽Singleton
26聽聽聽聽Singleton(const聽Singleton&);聽//聽Prevent聽clients聽from聽copying聽a聽Singleton
27聽聽聽聽Singleton&聽operator=(const聽Singleton&);
28private
:
29聽聽聽聽static
聽Mutex聽_mutex;
30聽聽聽聽static聽Singleton聽*_instance;聽//聽The聽one聽and聽only聽instance

31};
32

33#endif/*SINGLETON_H*/

singleton.cpp
1#include聽"singleton.h"
2
3Mutex聽Singleton::_mutex;
4Singleton*聽Singleton::_instance聽=0;
鐜板湪鐨凷ingleton铏界劧澶氱嚎紼嬪畨鍏紝鎬ц兘鍗村彈鍒頒簡褰卞搷銆備粠Instance()涓彲浠ョ湅鍒幫紝瀹為檯涓婁粎浠呭綋0 == _instance涓簍rue鏃舵墠闇瑕丩ock銆備綘寰堝鏄撳氨鍐欏嚭濡備笅浠g爜錛?br />
1static聽Singleton&聽Instance()聽{
2聽聽聽聽if聽(0==
聽_instance)聽{
3
聽聽聽聽聽聽聽聽Lock聽lock(_mutex);
4聽聽聽聽聽聽聽聽_instance聽=new
聽Singleton();
5
聽聽聽聽聽聽聽聽atexit(Destroy);
6
聽聽聽聽}
7聽聽聽聽return*
_instance;
8}
浣嗘槸榪欐牱榪樻槸浼?xì)漶旂敓绔炰簤鏉′?race condition)錛屼竴縐嶅箍涓轟漢鐭ョ殑鍋氭硶鏄嬌鐢ㄦ墍璋撶殑Double-Checked Locking錛?br />
聽1static聽Singleton&聽Instance()聽{
聽2聽聽聽聽if聽(0==
聽_instance)聽{
聽3
聽聽聽聽聽聽聽聽Lock聽lock(_mutex);
聽4聽聽聽聽聽聽聽聽if聽(0==
聽_instance)聽{
聽5聽聽聽聽聽聽聽聽聽聽聽聽_instance聽=new
聽Singleton();
聽6
聽聽聽聽聽聽聽聽聽聽聽聽atexit(Destroy);
聽7
聽聽聽聽聽聽聽聽}
聽8
聽聽聽聽}
聽9聽聽聽聽return*
_instance;
10}
Double-Checked Locking鏈哄埗鐪嬭搗鏉ュ儚鏄竴涓畬緹庣殑瑙e喅鏂規(guī)錛屼絾鏄湪鏌愪簺鏉′歡涓嬩粛鐒朵笉琛屻傜畝鍗曠殑璇達(dá)紝緙栬瘧鍣ㄤ負(fù)浜嗘晥鐜囧彲鑳戒細(xì)閲嶆帓鎸囦護(hù)鐨勬墽琛岄『搴?compiler-based reorderings)銆傜湅榪欎竴琛屼唬鐮侊細(xì)

_instance聽=new聽Singleton();

鍦ㄧ紪璇戝櫒鏈紭鍖栫殑鎯呭喌涓嬮『搴忓涓嬶細(xì)
1.new operator鍒嗛厤閫傚綋鐨勫唴瀛橈紱
2.鍦ㄥ垎閰嶇殑鍐呭瓨涓婃瀯閫燬ingleton瀵硅薄錛?br />3.鍐呭瓨鍦板潃璧嬪肩粰_instance銆?/span>

浣嗘槸褰撶紪璇戝櫒浼樺寲鍚庢墽琛岄『搴忓彲鑳藉涓嬶細(xì)
1.new operator鍒嗛厤閫傚綋鐨勫唴瀛橈紱
2.鍐呭瓨鍦板潃璧嬪肩粰_instance錛?br />3.鍦ㄥ垎閰嶇殑鍐呭瓨涓婃瀯閫燬ingleton瀵硅薄銆?/span>

褰撶紪璇戝櫒浼樺寲鍚庯紝濡傛灉綰跨▼涓鎵ц鍒?鍚庤鎸傝搗銆傜嚎紼嬩簩寮濮嬫墽琛屽茍鍙戠幇0 == _instance涓篺alse錛屼簬鏄洿鎺eturn錛岃岃繖鏃禨ingleton瀵硅薄鍙兘榪樻湭鏋勯犲畬鎴愶紝鍚庢灉...

涓婇潰璇寸殑榪樺彧鏄崟澶勭悊鍣ㄧ殑鎯呭喌錛屽湪澶氬鐞嗗櫒(multiprocessors)鐨勬儏鍐典笅錛岃秴綰跨▼鎶鏈繀鐒朵細(xì)娣峰悎鎵ц鎸囦護(hù)錛屾寚浠ょ殑鎵ц欏哄簭鏇存棤娉曚繚闅溿傚叧浜?span style="COLOR: #333399">Double-Checked Locking
鐨勬洿璇︾粏鐨勬枃绔狅紝璇風(fēng)湅錛?br />The "Double-Checked Locking is Broken" Declaration

5.浣跨敤volatile鍏抽敭瀛?br />涓轟簡璇存槑闂錛岃鍏堣冭檻濡備笅浠g爜錛?br />
聽1class聽MyThread聽:聽public聽Thread聽{
聽2public
:
聽3聽聽聽聽virtual聽void
聽run()聽{
聽4聽聽聽聽聽聽聽聽while聽(!
_stopped)聽{
聽5聽聽聽聽聽聽聽聽聽聽聽聽//do聽something

聽6聽聽聽聽聽聽聽聽}
聽7
聽聽聽聽}
聽8聽聽聽聽void
聽stop()聽{
聽9聽聽聽聽聽聽聽聽_stopped聽=true
;
10
聽聽聽聽}
11private
:
12
聽聽聽聽bool聽_stopped;
13
};
14

15聽...
16
17MyThread聽thread;
18thread.start();
涓婇潰鐢╰hread.start()寮鍚簡涓涓嚎紼嬶紝璇ョ嚎紼嬪湪while寰幆涓嫻媌ool鏍囪_stopped錛岀湅鏄惁璇ョ戶緇墽琛屻傚鏋滄兂瑕佺粨鏉熻繖涓嚎紼嬶紝璋冪敤thread.stop()搴旇娌¢棶棰樸備絾鏄渶瑕佹敞鎰忕殑鏄紪璇戝櫒寰堟湁鍙兘瀵筥stopped鐨勫瓨鍙栬繘琛屼紭鍖栥傚鏋滅紪璇戝櫒鍙戠幇_stopped琚綣佸瓨鍙?_stopped鍦╳hile寰幆涓?錛岀紪璇戝櫒鍙兘浼?xì)鑰冭檻灝哶stopped緙撳瓨鍒板瘎瀛樺櫒涓紝浠ュ悗_stopped灝嗕細(xì)鐩存帴浠庡瘎瀛樺櫒瀛樺彇銆傝繖鏃跺欏鏋滄煇涓嚎紼嬭皟鐢ㄤ簡thread.stop()錛屽_stopped鐨勪慨鏀瑰皢涓嶄細(xì)鍙嶆槧鍒板瘎瀛樺櫒涓紝thread灝嗕細(xì)姘歌繙寰幆涓嬪幓...

涓轟簡闃叉緙栬瘧鍣ㄤ紭鍖栵紝鐢╲olatile鍏抽敭瀛楀氨O(jiān)K浜嗭紝volatile璺焎onst鐨勭敤娉曞嚑涔庝竴鏍鳳紝鑳界敤const鐨勫湴鏂逛篃閮借兘鐢╲olatile銆傚Singleton鏉ヨ錛屼慨鏀瑰涓嬩袱澶勫嵆鍙細(xì)
1//singleton.h涓?/span>
2static聽Singleton聽*_instance;
3//鏀逛負(fù)

4static聽Singleton聽*volatile聽_instance;
5

6//singleton.cpp涓?/span>
7Singleton*聽Singleton::_instance聽=0;
8//鏀逛負(fù)

9Singleton*volatile聽Singleton::_instance聽=0;


6.灝哠ingleton娉涘寲涓烘ā鏉?br />singleton.h
聽1#ifndef聽SINGLETON_H
聽2
#define聽SINGLETON_H
聽3

聽4#include聽"synobj.h"
聽5
聽6template<class聽T>
聽7class聽Singleton聽{
聽8
聽聽聽聽CLASS_UNCOPYABLE(Singleton)
聽9public
:
10聽聽聽聽static聽T&聽Instance()聽{聽//聽Unique聽point聽of聽access

11聽聽聽聽聽聽聽聽if聽(0==聽_instance)聽{
12
聽聽聽聽聽聽聽聽聽聽聽聽Lock聽lock(_mutex);
13聽聽聽聽聽聽聽聽聽聽聽聽if聽(0==
聽_instance)聽{
14聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽_instance聽=new
聽T();
15
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽atexit(Destroy);
16
聽聽聽聽聽聽聽聽聽聽聽聽}
17
聽聽聽聽聽聽聽聽}
18聽聽聽聽聽聽聽聽return*
_instance;
19
聽聽聽聽}
20protected
:
21
聽聽聽聽Singleton(){}
22聽聽聽聽~
Singleton(){}
23private
:
24聽聽聽聽staticvoid聽Destroy()聽{聽//聽Destroy聽the聽only聽instance

25聽聽聽聽聽聽聽聽if聽(聽_instance聽!=0聽)聽{
26
聽聽聽聽聽聽聽聽聽聽聽聽delete聽_instance;
27聽聽聽聽聽聽聽聽聽聽聽聽_instance聽=0
;
28
聽聽聽聽聽聽聽聽}
29
聽聽聽聽}
30聽聽聽聽static
聽Mutex聽_mutex;
31聽聽聽聽static聽T聽*volatile聽_instance;聽//聽The聽one聽and聽only聽instance

32};
33

34template<class聽T>
35Mutex聽Singleton<T>::_mutex;
36

37template<class聽T>
38T聽*volatile聽Singleton<T>::_instance聽=0;
39

40#endif/*SINGLETON_H*/

嫻嬭瘯浠g爜錛?br />test.cpp
聽1#include聽"singleton.h"
聽2
聽3class聽A聽:聽public聽Singleton<A>聽{
聽4聽聽聽聽friend聽class聽Singleton<A>
;
聽5protected
:
聽6
聽聽聽聽A(){}
聽7聽聽聽聽~
A(){}
聽8public
:
聽9聽聽聽聽void
聽DoSomething(){}
10
};
11

12int聽main()聽{
13

14聽聽聽聽A聽&a聽=聽A::Instance();
15
聽聽聽聽a.DoSomething();
16

17聽聽聽聽return0;
18}


7.Singleton鐨勬瀽鏋勯棶棰?/strong>
鍒版Singleton宸茬粡綆楁瘮杈冨畬鍠勪簡錛屼絾鏄緷鐒剁畻涓嶄笂瀹岀編錛屽洜涓哄埌鐜板湪鍙槸瑙e喅浜嗗綰跨▼闂錛屽姞鍏ヤ簡妯℃澘鏀寔錛屽浜?span style="COLOR: #333399">KDL problem(The Dead Reference Problem)渚濈劧娌℃硶瑙e喅錛屽彲浠ヨ鍦ㄥ疄鐜癝ingleton妯″紡鏃訛紝鏈澶х殑闂灝辨槸澶氫釜鏈変緷璧栧叧緋葷殑Singleton鐨勬瀽鏋勯『搴?/span>銆傝櫧鐒?em style="COLOR: #333399">Modern C++ Design涓粰鍑轟簡瑙e喅鏂規(guī)錛屼絾鏄?span style="COLOR: #333399">Loki鐨勫疄鐜板お榪囧鏉傦紝鍦ㄦ灝變笉璇︾粏璇存槑浜嗭紝鏈夊叴瓚g殑鍙互鐪嬬湅Modern C++ Design錛屽綋鐒朵簡錛?span style="COLOR: #333399">Loki搴撲腑鐢ㄧ瓥鐣ユā寮忓疄鐜扮殑Singleton涔熷緢涓嶉敊錛?br />



]]>
闈炲畬緹嶤++ Singleton瀹炵幇[1]http://m.shnenglu.com/ant/archive/2007/09/07/31445.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 07 Sep 2007 06:49:00 GMThttp://m.shnenglu.com/ant/archive/2007/09/07/31445.htmlhttp://m.shnenglu.com/ant/comments/31445.htmlhttp://m.shnenglu.com/ant/archive/2007/09/07/31445.html#Feedback12http://m.shnenglu.com/ant/comments/commentRss/31445.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31445.htmlSingleton妯″紡鏄竴縐嶉潪甯哥畝鍗曠殑璁捐妯″紡錛岃繖縐嶆ā寮忓緢甯哥敤涔熷緢瀹規(guī)槗琚互鐢ㄣ傚綋浣犺璁″簲鐢ㄧ▼搴忕殑鏃跺欙紝緇忓父浼?xì)閬囧埌鏌愪簺瀵硅薄鍦ㄦ暣涓E嬪簭鐨勭敓鍛藉懆鏈熷簲璇ヤ粎鏈変竴涓疄渚嬬殑鎯呭喌錛屾瘮濡?span style="COLOR: #333399">File System錛孏raphic System錛孡ogging Utility錛岃繖鏃跺欏氨鍙互鐢ㄥ埌Singleton妯″紡銆係ingleton妯″紡鍦?span style="COLOR: #333399">GOF涓弿榪板涓嬶細(xì)

        Ensure a class only has one instance, and provide a global point of access to it.

Singleton妯″紡鐨勫畾涔夊緢綆鍗曪紝瀹炵幇涔熸湁N澶氱錛屼絾鏄嵈寰堥毦鎵懼埌涓涓О寰椾笂“瀹岀編”鐨勩傚疄鐜頒竴涓畬緹庣殑Singleton姣旀兂璞′腑瑕侀毦鐨勫錛屼笅闈㈡帰绱㈡х殑鏉ュ疄鐜頒竴涓潪瀹岀編鐨勩?br>
1.鍏稿瀷瀹炵幇
鍦–++涓紝Singleton妯″紡鐨勫吀鍨嬪疄鐜板涓嬶細(xì)

 1 // Header file Singleton.h
 2 class  Singleton {
 3 public
:
 4     static Singleton& Instance() { // Unique point of access

 5         if (0 ==  _instance)
 6             _instance = new
 Singleton();
 7         return *
_instance;
 8 
    }
 9     void
 DoSomething();
10 private
:
11     Singleton(); // Prevent clients from creating a new Singleton

12     ~Singleton(); // Prevent clients from deleting a Singleton
13     Singleton(const Singleton&); // Prevent clients from copying a Singleton
14     Singleton& operator=(const Singleton& );
15 private
:
16     static Singleton *_instance; // The one and only instance

17  };
18 

19 // Implementation file Singleton.cpp
20 Singleton* Singleton::_instance = 0;


閫氳繃灝哠ingleton鐨勬瀯閫犲嚱鏁拌涓簆rivate鍙互紱佹瀹㈡埛浠g爜鐩存帴鍒涘緩Singleton瀵硅薄錛岄櫎姝や箣澶栵紝Singleton鐨刢opy constructor鍜宑opy assignment operator閮戒負(fù)private涓斾粎鏈夊0鏄庢病鏈夊疄鐜幫紝紱佹浜嗗鎴蜂唬鐮佹嫹璐漇ingleton瀵硅薄銆傚敮涓鍙互鍒涘緩Singleton瀵硅薄鐨勬槸Singleton鑷繁鐨勯潤鎬佹垚鍛樺嚱鏁癐nstance錛岃繖鏍峰氨鍦ㄧ紪璇戝櫒淇濊瘉浜哠ingleton瀹炰緥鐨勫敮涓鎬с備笂闈㈣繖浜涙槸鍦–++涓疄鐜癝ingleton妯″紡鏈鍩烘湰鐨勮鐐廣?br>
Instance鏂規(guī)硶淇濊瘉鍙湁鍦ㄧ涓嬈¤皟鐢ㄦ椂鎵嶄細(xì)鐢熸垚Singleton瀵硅薄錛屼互鍚庣殑璋冪敤鍙槸綆鍗曡繑鍥炲敮涓鐨勫凡瀛樺湪鐨勫疄渚嬨侷nstance鏂規(guī)硶瀹為檯涓婂疄鐜扮殑鏄?span style="COLOR: #333399">鎳掓儼鍒濆鍖?lazy initialize)錛?/em>濡傛灉紼嬪簭涓牴鏈病鏈夌敤鍒癝ingleton瀵硅薄錛屼篃灝辨牴鏈笉浼?xì)漶旂敓Singleton鐨勫疄渚嬶紝榪欏湪Singleton瀵硅薄寰堝皯浣跨敤涓斿垱寤篠ingleton瀵硅薄寮閿姣旇緝澶х殑鎯呭喌涓嬬壒鍒湁鐢ㄣ?/p>

瀹㈡埛浠g爜鐜板湪鍙互榪欐牱浣跨敤Singleton錛?/p>

1 Singleton &=  Singleton::Instance();
2 s.DoSomething();


榪橀渶瑕佽鏄庣殑鏄疭ingleton鐨勬瀽鏋勫嚱鏁幫紝鏋愭瀯鍑芥暟涔熶負(fù)private鍙互紱佹瀹㈡埛鍐欏嚭濡備笅浠g爜銆傚鏋滄煇涓鎴峰啓鍑轟簡濡備笅浠g爜錛岄殢鍚庣殑瀵筍ingleton鐨勮闂氨浼?xì)瀵艰嚧湄?fù)瀹氫箟琛屼負(fù)錛屽洜涓篠ingleton瀵硅薄宸茬粡涓嶅瓨鍦ㄣ?br>

1 Singleton *= & Singleton::Instance();
2 delete p;


2.寮曞叆smart pointer
涓婇潰鐨勫疄鐜扮畻鏄竴涓ソ鐨勫疄鐜板悧錛熷綋鐒朵笉鏄紝鎴栬榪炰竴涓紜殑瀹炵幇閮界畻涓嶄笂銆傚鏋滀綘鎯沖噾鍚堬紝褰撶劧娌¢棶棰橈紝涓婇潰鐨勪唬鐮佸ぇ澶氭暟鎯呭喌涓嬪彲浠ュ伐浣滅殑寰堝ソ銆備篃璁鎬綘宸茬粡娉ㄦ剰鍒頒簡涓浜涢棶棰橈紝姣斿璇村湪涓婇潰鐨勪唬鐮佷腑鍙湁new娌℃湁delete銆傛槸鐨勶紝浣犺浼?xì)鍙戠敓memory leak瀵瑰惂錛屽叾瀹瀖emory leak閮戒笉鏄富瑕佺殑闂錛屾墍鏈夌殑鐜頒唬鎿嶄綔緋葷粺鍦ㄨ繘紼嬬粨鏉熺殑鏃跺欓兘浼?xì)瀵瑰唴瀛樺緢濂界殑杩涜鍥炴敹銆傛瘮memory leak鏇村煎緱璁╀漢鎷呭咖鐨勬槸resource leak錛屽鏋淪ingleton鍦ㄦ瀯閫犲嚱鏁頒腑璇鋒眰浜嗘煇浜涜祫婧愶細(xì)緗戠粶榪炴帴錛屾枃浠跺彞鏌勶紝鏁版嵁搴撹繛鎺ョ瓑銆傝繖浜涜祫婧愬皢寰椾笉鍒伴噴鏀俱?br>
鍞竴淇resource leak鐨勬柟娉曞氨鏄湪紼嬪簭緇撴潫鐨勬椂鍊檇elete _instance銆傚綋鐒朵簡錛岀敤smart pointer鍐嶅ソ涓嶈繃錛屽湪榪欓噷鐢╝uto_ptr灝卞彲浠ユ弧瓚抽渶瑕佷簡(濡傛灉浣犺繕涓嶇煡閬搒mart_ptr鏄粈涔堬紝鑺辯偣鏃墮棿鐔熸?zhèn)塁++鏍囧噯搴撳惂)錛屼慨鏀瑰悗鐨勪唬鐮佸涓嬶細(xì)

 1 // Header file Singleton.h
 2 class  Singleton {
 3 public
:
 4     static Singleton& Instance() { // Unique point of access

 5         if (0 ==  _instance.get())
 6             _instance.reset(new
 Singleton());
 7         return *
(_instance.get());
 8 
    }
 9     void
 DoSomething(){}
10 private
:
11     Singleton(){} // Prevent clients from creating a new Singleton

12     ~Singleton(){} // Prevent clients from deleting a Singleton
13     Singleton(const Singleton&); // Prevent clients from copying a Singleton
14     Singleton& operator=(const Singleton& );
15 private
:
16     friend auto_ptr<Singleton>
;
17     static auto_ptr<Singleton> _instance; // The one and only instance

18  };
19 

20 // Implementation file Singleton.cpp
21 auto_ptr<Singleton> Singleton::_instance;


3.鐢╝texit鏇挎崲smart pointer
C++騫舵病鏈夎瀹氫笉鍚?span style="COLOR: #333399">緙栬瘧鍗曞厓(translation unit錛岀畝鍗曡灝辨槸涓涓彲緙栬瘧鐨刢pp鏂囦歡)涓璼tatic瀵硅薄鐨勫垵濮嬪寲欏哄簭銆傚鏋滀竴涓▼搴忎腑鏈夊涓猄ingleton瀵硅薄錛岄偅涔堣繖浜汼ingleton瀵硅薄鐨勬瀽鏋勯『搴忎篃灝嗘槸浠繪剰鐨勩傚緢鏄劇劧錛屽綋澶氫釜Singleton瀵硅薄鏈変緷璧栧叧緋繪椂錛宻mart pointer鏍規(guī)湰鏃犳硶淇濊瘉Singleton鐨勬瀽鏋勯『搴忋?br>
msdn涓atexit鎻忚堪濡備笅錛?/p>

The atexit function is passed the address of a function (func) to be called when the program terminates normally. Successive calls to atexit create a register of functions that are executed in last-in, first-out (LIFO) order. The functions passed to atexit cannot take parameters. atexit  use the heap to hold the register of functions. Thus, the number of functions that can be registered is limited only by heap memory.

闇瑕佽鏄庣殑鏄痑texit騫朵笉姣攕mart pointer濂藉灝戯紝LIFO鐨勪繚璇佸浜庢湁澶嶆潅渚濊禆鍏崇郴鐨勫涓猄ingleton渚濈劧鏉熸墜鏃犲姏錛屼絾鏄敤atexit鏇挎崲smart pointer鍗存槸蹇呴』鐨勶紝瀹冩槸璁捐瀹岀編Singleton鐨勫熀紜銆?br>

#濡傛灉浣犵枒鎯慳texit涓轟粈涔堣繕鏄笉琛岋紝璇瘋冭檻涓嬮潰鐨勬儏鍐碉細(xì)
NOTE錛氫笅闈㈢殑鎯呭喌鍦?em>Modern C++ Design
涓彨鍋?em style="COLOR: #333399">KDL(Keyboard,Display,Log)problem銆?br>
鏌愪釜紼嬪簭涓嬌鐢ㄤ簡濡備笅3涓猄ingleton錛?span style="COLOR: #333399">Keyboard錛?span style="COLOR: #333399">Display錛?span style="COLOR: #333399">Log銆?span style="COLOR: #333399">Keyboard鍜?span style="COLOR: #333399">Display鍒嗗埆瀵瑰簲浜庤綆楁満鐨勯敭鐩樺拰鏄劇ず鍣紝Log鐢ㄦ潵璁板綍閿欒淇℃伅銆傚亣璁懼綋Keyboard鍜?span style="COLOR: #333399">Display鐨勬瀯閫犲嚱鏁板拰鏋愭瀯鍑芥暟鍑虹幇閿欒鏃朵細(xì)璋冪敤Log璁板綍閿欒淇℃伅錛屽茍涓旀瀯閫犲拰鏋愭瀯瀵艱嚧鐨勪換浣曢敊璇兘浼?xì)缁堟绋嬪簭銆?br>
鍦ㄧ▼搴忓惎鍔ㄦ椂錛屽鏋?span style="COLOR: #333399">Keyboard鏋勯犳垚鍔燂紝Display鏋勯犲け璐ワ紝寰堟樉鐒跺湪Display鐨勬瀯閫犲嚱鏁頒腑灝嗕細(xì)鏋勯?span style="COLOR: #333399">Log鑰屼笖澶辮觸淇℃伅浼?xì)琚?span style="COLOR: #333399">Log璁板綍錛屾牴鎹亣璁捐繖鏃跺欑▼搴忓噯澶囬鍑猴紝atexit娉ㄥ唽鐨勫嚱鏁板皢浼?xì)鎸塋IFO鐨勯『搴忚璋冪敤銆傚洜涓?span style="COLOR: #333399">Keyboard鍏堜簬Log鏋勯狅紝鎵浠?span style="COLOR: #333399">Log鍏堜簬Keyboard鏋愭瀯錛屼絾鏄綋鐢變簬鏌愮鍘熷洜Keyboard鍦ㄦ瀽鏋勬椂澶辮觸錛屾兂瑕佽皟鐢?span style="COLOR: #333399">Log璁板綍閿欒淇℃伅鏃訛紝Log鏃╁凡琚攢姣侊紝鍒橪og::Instance()灝嗕細(xì)瀵艱嚧鏈畾涔夎涓恒?br>

#atexit鐨勪弗閲嶉棶棰橈細(xì)
浠庝笂闈㈢殑渚嬪瓙鍙互鐪嬪嚭錛宎texit鍜宻mart pointer鐩告瘮浠呬粎鏄湁LIFO鐨勪繚璇佽屽凡錛岃繖鏍風(fēng)殑淇濊瘉璨屼技涔熶笉鎬庝箞鏈夋晥錛屽洜涓篴texit璺焥mart pointer涓鏍蜂篃鏃犳硶瑙e喅KDL probleam銆?br>
atexit鐢變簬LIFO甯︽潵浜嗗彟澶栫殑闂錛岀湅涓嬮潰鐨勪唬鐮侊細(xì)

 1 #include <cstdlib>
 2 void  Bar() {
 3 
    ...
 4 
}
 5 void
 Foo() {
 6 
    std::atexit(Bar);
 7 
}
 8 int
 main() {
 9 
    std::atexit(Foo);
10     return 0
;
11 }

涓婇潰鐨勫皬孌典唬鐮佺敤atexit娉ㄥ唽浜咶oo錛孎oo璋冪敤浜唖td::atexit(Bar)銆傚綋紼嬪簭閫鍑烘椂錛屾牴鎹產(chǎn)texit鐨凩IFO淇濊瘉錛孊ar鍦‵oo涔嬪悗娉ㄥ唽錛屽洜姝ar搴旇鍦‵oo涔嬪墠璋冪敤錛屼絾鏄綋Bar娉ㄥ唽鐨勬椂鍊橣oo宸茬粡璋冪敤浜嗭紝Bar鏍規(guī)湰灝辨病鏈夋満浼?xì)鑳藉鍦‵oo涔嬪墠璋冪敤銆傝繖鏄庢樉鑷浉鐭涚浘瀵瑰惂錛屾病鍔炴硶錛孋++鏍囧噯濂藉儚蹇借浜嗚繖涓鐐癸紝鍥犳濡傛灉綾諱技浠g爜琚皟鐢紝鑲畾涓嶄細(xì)鏈変粈涔堝ソ鐨勭粨鏋滐紝濂戒竴鐐規(guī)槸resource leak錛屽樊涓鐐逛及璁$▼搴忓氨宕╂簝浜嗭紒錛侊紒

atexit鐨勮繖涓棶棰樿窡Singleton鏈夊叧緋誨悧錛熷綋鐒舵湁錛屽鏋滃湪涓涓猄ingleton鐨勬瀽鏋勫嚱鏁頒腑璋冪敤atexit灝變細(xì)鍑虹幇涓婅堪闂銆傚嵆鍦?span style="COLOR: #333399">KDL problem涓紝濡傛灉Keyboard鍜?span style="COLOR: #333399">Display閮芥瀯閫犳垚鍔燂紝褰?span style="COLOR: #333399">Keyboard鎴?span style="COLOR: #333399">Display浠繪剰涓涓瀽鏋勫け璐ユ椂錛?span style="COLOR: #333399">Keyboard鎴?span style="COLOR: #333399">Display鍦ㄦ瀽鏋勫嚱鏁頒腑浼?xì)鏋勯?span style="COLOR: #333399">Log錛?span style="COLOR: #333399">Log鐨勬瀯閫犲嚱鏁頒細(xì)闂存帴璋冪敤atexit銆俹ops!!!錛屽彲鎬曠殑鏈畾涔夎涓恒?br>
鐪嬪埌榪欓噷浣犱竴瀹氬atexit鐩稿綋澶辨湜錛岃矊浼煎畠甯︽潵鐨勫ソ澶勫浜庡潖澶勩備絾鏄浣犵浉淇★紝濡傛灉閫傚綋璁捐錛宎texit鍦ㄥ悗闈㈢殑Singleton鏀歸犱腑浼?xì)钃v鍒板緢閲嶈鐨勪綔鐢ㄣ?br>
鐢╝texit鍚庣殑浠g爜錛?/p>

 1 // Header file Singleton.h
 2 class  Singleton {
 3 public
:
 4     static Singleton& Instance() { // Unique point of access

 5         if (0 ==  _instance) {
 6             _instance = new
 Singleton();
 7             atexit(Destroy); // Register Destroy function

 8          }
 9         return *
_instance;
10 
    }
11     void
 DoSomething(){}
12 private
:
13     static void Destroy() { // Destroy the only instance

14         if ( _instance != 0  ) {
15 
            delete _instance;
16             _instance = 0
;
17 
        }
18 
    }
19     Singleton(){} // Prevent clients from creating a new Singleton

20     ~Singleton(){} // Prevent clients from deleting a Singleton
21     Singleton(const Singleton&); // Prevent clients from copying a Singleton
22     Singleton& operator=(const Singleton& );
23 private
:
24     static Singleton *_instance; // The one and only instance

25  };
26 

27 // Implementation file Singleton.cpp
28 Singleton* Singleton::_instance = 0;

浣犳湁娌℃湁浠旂粏鑰冭檻榪嘍estroy涓殑_instance = 0;榪欎竴琛屼唬鐮侊紝涓婅堪浠g爜瀹為檯涓婂疄鐜扮殑鏄?em style="COLOR: #333399">涓嶆楦熸ā寮?The Phoenix Singleton)錛?/em>鎵璋撲笉姝婚笩錛屽氨璺熶竴杈変竴鏍峰彲浠ユ鑰屽鐢熴備笂闈㈢殑浠g爜鍙互瑙e喅鏈枃鏈鏃╂彁鍑虹殑KDL problem錛屽嵆濡傛灉Keyboard鏋愭瀯澶辮觸錛岃櫧鐒?span style="COLOR: #333399">Log宸茬粡鏋愭瀯錛屼絾鏄敱浜嶥estroy涓殑_instance = 0;榪欎竴琛屼唬鐮侊紝Log::Instance()灝嗕細(xì)鍒涘緩涓涓柊鐨?span style="COLOR: #333399">Log瀵硅薄錛岀▼搴忓皢浼?xì)琛ㄧ幇鑹ソ銆傚綋鐒朵簡錛?span style="COLOR: #993366">Phoenix Singleton浠呰兘鐢ㄤ簬鏃犵姸鎬佺殑Singleton錛屽鏋?span style="COLOR: #333399">Log闇瑕佷繚瀛樻煇浜涚姸鎬侊紝Phoenix Singleton涔熶笉浼?xì)甯︽潵鋼Q浣曞ソ澶勩備綘褰撶劧鍙互鐢ㄦ煇浜涙柟娉曠淮鎸?span style="COLOR: #993366">Phoenix Singleton鐨勭姸鎬侊紝浣嗘槸鍦ㄥ仛涔嬪墠鍏堟兂鎯崇湅鏄惁鍊煎緱錛岀淮鎸佺姸鎬佸彲鑳戒細(xì)浣縎ingleton鍙樺緱鐗瑰埆澶嶆潅銆?br>
涓婇潰鐨?span style="COLOR: #993366">Phoenix Singleton宸茬粡鍙互婊¤凍澶ч儴鍒嗛渶瑕侊紝濡傛灉浣犵殑Singleton娌℃湁娑夊強(qiáng)鍒板綰跨▼錛屽涓猄ingleton涔嬮棿涔熸病鏈変緷璧栧叧緋伙紝浣犲ぇ鍙互鏀懼績浣跨敤銆備絾鏄鏋滀綘鐢ㄥ埌澶氱嚎紼嬶紝鎴栬呬綘鐨凷ingleton鍏崇郴濡?span style="COLOR: #333399">KDL鑸鏉傦紝鎴栬呬綘瑙夊緱瀵規(guī)瘡涓涓猄ingleton閮芥暡鍚屾牱鐨勪唬鐮佽浣犲帉鐑︺傚湪鍚庨潰鍑犵瘒浼?xì)鏈変竴涓?span style="COLOR: #993366">澶氱嚎紼嬪畨鍏?/span>鐨勶紝鑳藉瑙e喅澶氫釜Singleton渚濊禆鍏崇郴鐨勶紝鍩轟簬妯℃澘鐨凷ingleton瀹炵幇銆?br>

]]>
浠嶹in32 API灝佽Thread綾籟2]http://m.shnenglu.com/ant/archive/2007/08/31/31280.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Fri, 31 Aug 2007 03:58:00 GMThttp://m.shnenglu.com/ant/archive/2007/08/31/31280.htmlhttp://m.shnenglu.com/ant/comments/31280.htmlhttp://m.shnenglu.com/ant/archive/2007/08/31/31280.html#Feedback7http://m.shnenglu.com/ant/comments/commentRss/31280.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31280.html(functor聽and function object)浣滀負(fù)鏋勯犲嚱鏁板弬鏁般傚鏋滀綘鐔熸?zhèn)塖TL錛屼綘搴旇鐔熸?zhèn)塨ind1st鍜宐ind2nd榪欎袱涓嚱鏁伴厤鎺ュ櫒(function adapter)錛宐ind1st鍜宐ind2nd鍙互灝嗕竴涓簩鍏冨嚱鏁?binary function)杞崲鎴愪竴鍏冨嚱鏁?unary function)銆備負(fù)浜嗕嬌Thread綾昏兘澶熺敤鏅氬嚱鏁板拰鍑芥暟瀵硅薄浣滀負(fù)鍙傛暟錛屾垜浠渶瑕佷竴涓猙ind灝嗕竴鍏冨嚱鏁拌漿鎹㈡垚鏃犲弬鍑芥暟錛?br />bind.h
聽1#ifndef聽BIND_H
聽2
#define聽BIND_H
聽3

聽4template聽<class聽_Result>
聽5struct聽trivial_function聽{
聽6
聽聽聽聽typedef聽_Result聽result_type;
聽7
};
聽8

聽9template聽<class聽_Operation>
10class聽binder聽:聽public聽trivial_function<typename聽_Operation::result_type>聽{
11public
:
12聽聽聽聽binder(const聽_Operation& x,聽const聽typename聽_Operation::argument_type&
y)
13
聽聽聽聽聽聽聽聽:op(x),聽value(y)聽{}
14聽聽聽聽typename聽_Operation::result_type聽operator()()聽const
聽{
15聽聽聽聽聽聽聽聽return
聽op(value);
16
聽聽聽聽}
17protected
:
18
聽聽聽聽_Operation聽op;
19
聽聽聽聽typename聽_Operation::argument_type聽value;
20
};
21

22template聽<class聽_Operation,聽class聽_Tp>
23inline聽binder<_Operation>
24bind(const聽_Operation&聽__fn,聽const聽_Tp&
聽__x)聽
25
{
26
聽聽聽聽typedef聽typename聽_Operation::argument_type聽_Arg_type;
27聽聽聽聽return聽binder<_Operation>
(__fn,聽_Arg_type(__x));
28
}
29

30#endif/*BIND_H*/

鏈変簡bind鎴戜滑榪橀渶瑕佷慨鏀筎hread綾葷殑鏋勯犲嚱鏁幫紝鏄劇劧鎴戜滑蹇呴』灝嗘瀯閫犲嚱鏁板0鏄庝負(fù)鎴愬憳妯℃澘(榪樻湁涓縐嶆柟娉曚篃鍙互杈懼埌鍚屾牱鐨勭洰鐨勶紝灝辨槸鎶奣hread綾誨0鏄庝負(fù)妯℃澘錛屼絾鏄繖鏍風(fēng)殑璁捐濂藉儚涓嶅お濂?錛岃繖鏍鋒墠鑳藉璁㏕hread綾葷殑鏋勯犲嚱鏁板彲浠ユ帴鍙楀悇縐嶇被鍨嬬殑鍙傛暟錛屼慨鏀瑰悗鐨勬瀯閫犲嚱鏁板簲璇ヨ兘澶熶嬌鐢ㄥ涓嬩笁縐嶇被鍨嬬殑鍙傛暟錛?br />1.Runnable *
2.no argument function
3.no argument functor
涓嬮潰鏄慨鏀瑰悗鐨勫ご鏂囦歡錛?br />runnable.h
聽1#ifndef聽RUNNABLE_H
聽2
#define聽RUNNABLE_H
聽3

聽4struct聽Runnable聽{
聽5聽聽聽聽virtual聽void聽run()聽=0
;
聽6聽聽聽聽virtual聽~
Runnable()聽{}
聽7
};
聽8

聽9template聽<class聽T>
10class聽RunnableFunctor聽:聽public聽Runnable聽{
11public
:
12聽聽聽聽RunnableFunctor(const聽T&
f)聽:_func(f)聽{}
13聽聽聽聽virtual聽void
聽run()聽{聽_func();聽}
14private
:
15
聽聽聽聽T聽_func;
16
};
17

18//base聽template聽for聽no聽argument聽functor
19template聽<class聽T>
20struct聽FuncImpl聽{
21聽聽聽聽static聽Runnable*聽transfer(const聽T&
t)聽{
22聽聽聽聽聽聽聽聽returnnew聽RunnableFunctor<T>
(t);
23
聽聽聽聽}
24
};
25

26//partial聽specialization聽for聽T*
27template聽<class聽T>
28struct聽FuncImpl<T*>聽{
29聽聽聽聽static聽Runnable*聽transfer(T*
t)聽{
30聽聽聽聽聽聽聽聽return
聽t;
31
聽聽聽聽}
32
};
33

34//partial聽specialization聽for聽no聽argument聽function
35template聽<class聽T>
36struct聽FuncImpl<T聽(*)()>聽{
37聽聽聽聽static聽Runnable*聽transfer(T聽(*
t)())聽{
38聽聽聽聽聽聽聽聽returnnew聽RunnableFunctor<T聽(*)()>
(t);
39
聽聽聽聽}
40
};
41

42template聽<class聽T>
43inline聽Runnable*聽transfer(const聽T& t)聽{
44聽聽聽聽return聽FuncImpl<T>
::transfer(t);
45
}
46

47#endif/*RUNNABLE_H*/

thread.h
聽1#ifndef聽THREAD_H
聽2
#define聽THREAD_H
聽3

聽4#include聽<windows.h>
聽5#include聽"bind.h"
聽6#include聽"runnable.h"
聽7
聽8#define聽CLASS_UNCOPYABLE(classname)聽\
聽9聽聽聽聽private
:聽\
10聽聽聽聽classname(const聽classname&
);聽\
11聽聽聽聽classname&聽operator=(const聽classname&
);
12

13class聽Thread聽:聽public聽Runnable聽{
14
聽聽聽聽CLASS_UNCOPYABLE(Thread)
15public
:
16
聽聽聽聽Thread()
17聽聽聽聽聽聽聽聽:_target(0
)
18聽聽聽聽聽聽聽聽,_handle(0
)聽{
19

20聽聽聽聽}
21聽聽聽聽template聽<class聽T>

22聽聽聽聽explicit聽Thread(const聽T& op)
23
聽聽聽聽聽聽聽聽:_target(transfer(op))
24聽聽聽聽聽聽聽聽,_handle(0
)聽{
25

26聽聽聽聽}
27聽聽聽聽virtual聽~
Thread();
28聽聽聽聽virtual聽void
聽run()聽{}
29聽聽聽聽void
聽start();
30聽聽聽聽void
聽join();
31private
:
32聽聽聽聽static聽unsigned聽__stdcall聽threadProc(void*
param);
33private
:
34聽聽聽聽Runnable*
_target;
35
聽聽聽聽HANDLE聽_handle;
36
};
37

38#endif/*THREAD_H*/

thread.cpp鍜屽墠涓綃囩殑鍑犱箮涓鏍鳳紝鍞竴鐨勪笉鍚屾槸鍘繪帀浜嗘瀯閫犲嚱鏁癟hread(Runnable *)錛屽洜涓虹幇鍦ㄧ殑鏋勯犲嚱鏁版敼鎴愪簡鎴愬憳妯℃澘錛屽疄鐜頒篃鏀懼湪thread.h涓簡銆傜幇鍦ㄧ殑鏋勯犲嚱鏁拌兘澶熸帴鍙楀悇縐嶇被鍨嬬殑鍙傛暟錛屼富瑕佸綊鍔熶簬妯℃澘鍑芥暟transfer錛屽疄鐜頒唬鐮佸湪runnable.h涓紝涓昏鎶宸ф槸鐢ㄧ被鐨勫亸鐗瑰寲妯℃嫙鍑芥暟妯℃澘鐨勫亸鐗瑰寲錛屼笉鏄庣櫧鐨勮鐪?a id="_117bf2a46b69_HomePageDays_DaysList_ctl00_DayItem_DayList_ctl01_TitleUrl" href="/ant/archive/2007/08/30/31201.html">涓轟粈涔堜笉瑕佺壒鍖栧嚱鏁版ā鐗?/a>銆?br />
涓嬮潰鏄祴璇曚唬鐮侊細(xì)
test.cpp
聽1#include聽"thread.h"
聽2#include聽<iostream>
聽3#include聽<functional>
聽4
聽5using聽namespace聽std;
聽6

聽7//no聽argument聽function
聽8void聽print()聽{
聽9聽聽聽聽cout聽<<"print"<<
聽endl;
10
}
11

12//unary聽function
13void聽print1(int聽n)聽{
14聽聽聽聽cout聽<<"print1"<<
聽endl;
15
}
16

17//binary聽function
18void聽print2(int聽m,聽int聽n)聽{
19聽聽聽聽cout聽<<"print2"<<
聽endl;
20
}
21

22
23//no聽argument聽functor
24struct聽PrintFunctor聽{
25聽聽聽聽void聽operator()()聽const
聽{
26聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor"<<
聽endl;
27
聽聽聽聽}
28
};
29

30//unary聽functor
31struct聽PrintFunctor1聽:聽public聽unary_function<int,聽void>聽{
32聽聽聽聽void聽operator()(int聽n)聽const
聽{
33聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor1"<<
聽endl;
34
聽聽聽聽}
35
};
36

37//binary聽functor
38struct聽PrintFunctor2聽:聽public聽binary_function<int,聽int,聽void>聽{
39聽聽聽聽void聽operator()(int聽m,聽int聽n)聽const
聽{
40聽聽聽聽聽聽聽聽cout聽<<"PrintFunctor2"<<
聽endl;
41
聽聽聽聽}
42
};
43

44int聽main()聽{
45

46聽聽聽聽//construct聽Thread聽with聽no聽argument聽function
47聽聽聽聽Thread聽thread1(&print);
48
聽聽聽聽thread1.start();
49

50聽聽聽聽//construct聽Thread聽with聽unary聽function
51聽聽聽聽Thread聽thread2(bind(ptr_fun(print1),聽5));
52
聽聽聽聽thread2.start();
53

54聽聽聽聽//construct聽Thread聽with聽binary聽function
55聽聽聽聽Thread聽thread3(bind(bind1st(ptr_fun(print2),聽1),聽2));
56
聽聽聽聽thread3.start();
57

58
59聽聽聽聽//construct聽Thread聽with聽no聽argument聽functor
60聽聽聽聽Thread聽thread4((PrintFunctor()));
61
聽聽聽聽thread4.start();
62

63聽聽聽聽//construct聽Thread聽with聽unary聽functor
64聽聽聽聽Thread聽thread5(bind(PrintFunctor1(),聽5));
65
聽聽聽聽thread5.start();
66

67聽聽聽聽//construct聽Thread聽with聽binary聽functor
68聽聽聽聽Thread聽thread6(bind(bind1st(PrintFunctor2(),聽1),聽2));
69
聽聽聽聽thread6.start();
70

71聽聽聽聽thread1.join();
72
聽聽聽聽thread2.join();
73
聽聽聽聽thread3.join();
74
聽聽聽聽thread4.join();
75
聽聽聽聽thread5.join();
76
聽聽聽聽thread6.join();
77

78聽聽聽聽return0;
79}

褰撶劧浜嗭紝涓婇潰鐨勫茍涓嶆槸鍏ㄩ儴錛屼慨鏀瑰悗鐨凾hread綾諱笉浠呰兘澶熶嬌鐢ㄥ師鍏堢殑浠嶵hread綾葷戶鎵挎垨鑰呭疄鐜癛unnable鎺ュ彛鐨勬柟娉曪紝榪樺彲浠ヤ嬌鐢ㄤ換浣曟棤鍙傚嚱鏁版垨鏃犲弬鍑芥暟瀵硅薄銆傞櫎浜唗est.cpp閲岀ず鑼冪殑錛屼綘鐢氳嚦鍙互鐢╞ind,bind1st,bind2st,mem_fun,mem_fun_ref鐨勭粍鍚堟潵鐢ㄦ煇涓被鐨勬垚鍛樺嚱鏁頒綔涓哄弬鏁幫紝鍏鋒湁瓚呭己鐨勭伒媧繪с?br />
鐩墠瀹炵幇鐨勮繖浜涢兘鏄疶hread綾繪渶鍩烘湰鐨勫姛鑳斤紝鍏朵粬鍔熻兘濡傝緗嚎紼嬩紭鍏堢駭錛屾寕璧鋒垨鎭㈠綰跨▼錛屽紓甯稿鐞嗙瓑鍏蜂綋瀹炵幇閮芥瘮杈冪畝鍗曪紝榪欒繖閲屽氨涓嶄竴涓瀹炵幇浜嗐?
婧愪唬鐮佷笅杞斤細(xì)鐐瑰嚮涓嬭澆


]]>
C++&Win32鍐欑殑絀哄綋鎺ラ緳http://m.shnenglu.com/ant/archive/2007/08/30/31234.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Thu, 30 Aug 2007 13:37:00 GMThttp://m.shnenglu.com/ant/archive/2007/08/30/31234.htmlhttp://m.shnenglu.com/ant/comments/31234.htmlhttp://m.shnenglu.com/ant/archive/2007/08/30/31234.html#Feedback19http://m.shnenglu.com/ant/comments/commentRss/31234.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31234.html絀哄綋鎺ラ緳娓告垙錛屽啓浜嗕竴涓ぜ鎷滐紝瀹屽叏浠縲indows鐨勫懙鍛點?br>涓嶈繃涔熶笉鍏ㄤ竴鏍鳳紝鏈変竴浜涚粏鑺備竴鐩存病鏈夋椂闂村紕錛屾病鍔炴硶鏈榪戞瘮杈冩噿...

涓巜indows涓嬬殑鐩告瘮錛屾垜鍋氱殑榪欎釜鍔犲叆浜?span style="COLOR: #993300">AI(蹇嵎閿瓼6)錛屽彲浠ュ紑鍚?span style="COLOR: #993300">縐葷墝鎻愮ず(蹇嵎閿瓼7)錛屼笉榪?span style="COLOR: #993300">鎾ら攢涓鐩存病鏈夊仛錛屽彲鑳藉摢澶╃獊鐒舵兂鍋氫簡浼?xì)鍔犱笂鍘汇傚彟澶栨敼榪涗簡搴熺墝鍥炴敹綆楁硶錛岄潪娉曠Щ鐗岀殑鎻愮ず鐩存帴鍘繪帀浜嗭紝榪樻湁涓浜涘皬鐨勭粏鑺備綔浜嗘敼榪涖?br>鎰熷叴瓚g殑鍙互涓嬭澆錛?a title=鐐瑰嚮涓嬭澆 href="http://m.shnenglu.com/Files/ant/Freecell.rar">鐐瑰嚮涓嬭澆
婧愪唬鐮佷笅杞斤細(xì)鐐瑰嚮涓嬭澆
欏轟究鍙戝嚑寮犳埅鍥撅細(xì)







]]>
涓轟粈涔堜笉瑕佺壒鍖栧嚱鏁版ā鐗?/title><link>http://m.shnenglu.com/ant/archive/2007/08/30/31201.html</link><dc:creator>铓傝殎緇堢粨鑰?/dc:creator><author>铓傝殎緇堢粨鑰?/author><pubDate>Thu, 30 Aug 2007 05:55:00 GMT</pubDate><guid>http://m.shnenglu.com/ant/archive/2007/08/30/31201.html</guid><wfw:comment>http://m.shnenglu.com/ant/comments/31201.html</wfw:comment><comments>http://m.shnenglu.com/ant/archive/2007/08/30/31201.html#Feedback</comments><slash:comments>4</slash:comments><wfw:commentRss>http://m.shnenglu.com/ant/comments/commentRss/31201.html</wfw:commentRss><trackback:ping>http://m.shnenglu.com/ant/services/trackbacks/31201.html</trackback:ping><description><![CDATA[ <p> <font face="Verdana" color="#006666">Overloading vs. Specialization</font> <br /> <br />鍦–++涓湁<em>class templates</em> 鍜?em>function templates</em>錛岃繖涓ょ妯$増鏈夊緢澶氬尯鍒紝鏈閲嶈鐨勫尯鍒氨鏄噸杞?overloading)錛?br />鏅氱殑C++綾諱笉鑳介噸杞斤紝褰撶劧綾繪ā鐗堜篃涓嶈兘閲嶈澆錛涚浉鍙嶏紝鏅氬嚱鏁板彲浠ラ噸杞斤紝鍑芥暟妯$増涔熻兘閲嶈澆銆傝繖鍐嶆甯鎬笉榪囷紝鐪嬩笅闈㈢殑浠g爜錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1:聽Class聽vs.聽function聽template,聽and聽overloading聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽class聽template</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">聽};聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(a)<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽function聽template聽with聽two聽overloads</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">,聽T,聽</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">聽);聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(c)<br /></span> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span></font> </div> <p>鍍忎笂闈㈡湭鐗瑰寲鐨勬ā鏉塊氬父鍙仛<em>base templates</em>銆傚綋鐒訛紝<em>base templates</em>鑳藉琚壒鍖栵紝鍦ㄧ壒鍖栬繖涓鐐逛笂<br /><em>class templates</em> 鍜?em>function templates</em>鏈夊緢澶х殑鍖哄埆錛氫竴涓?em>class template</em> 鑳藉琚?em>partially specialized</em> and/or <br /><em>fully specialized</em>錛屼竴涓?em>function template</em>鍙兘琚?em>fully specialized</em>錛屼絾鏄敱浜?em>function template</em>s鑳藉閲嶈澆鎴戜滑鍙互閫氳繃閲嶈澆鏉ュ疄鐜板拰<em>partially specialized</em> 鐩稿綋鐨勫姛鑳姐備笅闈㈢殑浠g爜璇存槑浜嗚繖浜涘尯鍒細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1,聽continued:聽Specializing聽templates聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽partial聽specialization聽of聽(a)聽for聽pointer聽types聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">*></span><span style="COLOR: #000000">聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽};<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽full聽specialization聽of聽(a)聽for聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽X</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽{聽</span><span style="COLOR: #008000">/*...</span><span style="COLOR: #008000"></span><span style="COLOR: #008000">*/</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽};<br /></span> <span style="COLOR: #008080">聽9</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽A聽separate聽base聽template聽that聽overloads聽(b)聽and聽(c)聽<br /></span> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽--聽NOT聽a聽partial聽specialization聽of聽(b),聽because聽<br /></span> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽there's聽no聽such聽thing聽as聽a聽partial聽specialization聽<br /></span> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽of聽a聽function聽template!聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(d)<br /></span> <span style="COLOR: #008080">15</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽A聽full聽specialization聽of聽(b)聽for聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">17</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">(聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(e)<br /></span> <span style="COLOR: #008080">18</span>聽</font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">19</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽A聽plain聽old聽function聽that聽happens聽to聽overload聽with聽<br /></span> <span style="COLOR: #008080">20</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(b),聽(c),聽and聽(d)聽--聽but聽not聽(e),聽which聽we'll聽<br /></span> <span style="COLOR: #008080">21</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽discuss聽in聽a聽moment聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">22</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽</span><span style="COLOR: #0000ff">double</span><span style="COLOR: #000000">聽);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽(f)<br /></span> <span style="COLOR: #008080">23</span>聽<span style="COLOR: #008000"></span></font> </div> <p>鏍規(guī)嵁鍑芥暟閲嶈澆瑙f瀽瑙勫垯錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽1,聽continued:聽Overload聽resolution聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span></font> <font face="Courier New"> <span style="COLOR: #000000">bool聽b;聽<br /></span> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽i;聽<br /></span> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">double</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽d;<br /></span> <span style="COLOR: #008080">聽6</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #000000">f(聽b聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(b)聽with聽T聽=聽bool聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽i,聽</span><span style="COLOR: #000000">42</span><span style="COLOR: #000000">,聽d聽);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(c)聽with聽T聽=聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">i聽);聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(d)聽with聽T聽=聽int聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽i聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(e)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">f(聽d聽);聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(f)</span></font> </div> <p>涓婇潰璇寸殑榪欎簺鍏跺疄閮芥槸寰堢畝鍗曠殑鎯呭喌錛屽ぇ澶氭暟浜哄緢瀹規(guī)槗灝辮兘鏄庣櫧錛屼笅闈㈢殑鎵嶆槸瀹規(guī)槗璁╀漢寮勬販鐨勶細(xì)<br /><br />1.鑰冭檻濡備笅浠g爜錛?/p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽2:聽Explicit聽specialization聽<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(a)聽a聽base聽template聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽f(聽T聽);<br /></span> <span style="COLOR: #008080">聽5</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">聽);聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽聽聽聽聽(function聽templates聽can't聽be聽partially聽<br /></span> <span style="COLOR: #008080">聽8</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽聽聽聽聽specialized;聽they聽overload聽instead)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(c)聽explicit聽specialization聽of聽(b)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">12</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽...</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">p;聽<br /></span> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #000000">f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽calls聽(c)</span></font> </div> <p>鏈鍚庝竴琛岀殑緇撴灉鍍忓ぇ澶氭暟浜烘墍鏈熸湜鐨勪竴鏍鳳紝闂鏄細(xì)涓轟粈涔堟湡鏈涙槸榪欎釜緇撴灉錛?br />濡傛灉浣犳湡鏈涚殑鍘熷洜鏄敊璇殑錛屾帴涓嬫潵鐨勪竴瀹氫細(xì)璁╀綘濂藉銆備篃璁鎬綘浼?xì)璇村Q?鎴戜負(fù)<span style="COLOR: #3366ff">int*</span>鍐欎簡涓涓壒鍖栫増鏈紝<span style="COLOR: #3366ff">f(p)</span>褰撶劧浼?xì)璋冪?span style="COLOR: #3366ff">c</span>"錛屼笉騫哥殑鏄紝榪欐鏄敊璇殑鍘熷洜!!!<br /><br />2.鍐嶈冭檻涓嬮潰鐨勪唬鐮侊細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽Example聽3<br /></span> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(a)聽same聽old聽base聽template聽as聽before聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽f(聽T聽);<br /></span> <span style="COLOR: #008080">聽5</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(c)聽explicit聽specialization,聽this聽time聽of聽(a)</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽7</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f</span><span style="COLOR: #000000"><></span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">);<br /></span> <span style="COLOR: #008080">聽8</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽9</span>聽<span style="COLOR: #000000">template</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span><span style="COLOR: #000000">聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽(b)聽a聽second聽base聽template,聽overloads聽(a)聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">聽f(聽T</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽);<br /></span> <span style="COLOR: #008080">11</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽...</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #008000"></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">p;聽<br /></span> <span style="COLOR: #008080">15</span>聽<span style="COLOR: #000000">f(聽p聽);聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽calls聽(b)!聽overload聽resolution聽ignores聽<br /></span> <span style="COLOR: #008080">16</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span></font> <font face="Courier New"> <span style="COLOR: #008000">聽specializations聽and聽operates聽on聽the聽base聽<br /></span> <span style="COLOR: #008080">17</span>聽<span style="COLOR: #008000">聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">聽function聽templates聽only</span></font> </div> <p>濡傛灉榪欎釜緇撴灉璁╀綘鎰熷埌鎯婂錛岄偅灝卞浜嗭紒寰堝浜洪兘浼?xì)鎰熷埌鎯婂锛?br />鐞嗚В榪欎釜鐨勫叧閿槸錛?em style="COLOR: #993300">Specializations don't overload,only the base templates overload.<br /></em><br />閲嶈澆瑙f瀽浠呬粎閫夋嫨base template(鎴栬卬ontemplate function錛屽鏋滄湁鐨勮瘽)錛屽彧鏈夊綋緙栬瘧鍣ㄥ凡緇忓喅瀹氫簡鍝釜<br />base template灝嗕細(xì)琚夋嫨錛岀紪璇戝櫒鎵嶄細(xì)緇х畫寰涓嬪鎵鵑傚悎鐨勭壒鍖栫増鏈紝濡傛灉鎵懼埌浜嗗氨浣跨敤閭d釜鐗瑰寲鐗堟湰銆?br /><br />鏈鍚庯紝搴斿綋閬垮厤鐗瑰寲鍑芥暟妯℃澘錛屼篃瑕侀伩鍏嶉噸杞藉嚱鏁版ā鏉?nontemplate function鐨勯噸杞藉綋鐒舵病闂)銆傚鏋滀竴瀹氳榪欐牱錛屽彲浠ヤ嬌鐢ㄥ涓嬫柟娉曟ā鎷熷嚱鏁版ā鏉跨殑鍋忕壒鍖栵細(xì)</p> <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"> <font face="Courier New"> <span style="COLOR: #008080">聽1</span>聽<span style="COLOR: #008000">//</span><span style="COLOR: #008000">base聽template聽class,聽</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽2</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽3</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">struct聽FuncImpl聽{<br /></span> <span style="COLOR: #008080">聽4</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">users,聽go聽ahead聽and聽specialize聽this</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">聽5</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽T聽</span><span style="COLOR: #000000">&</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">聽6</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">0</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">聽7</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">聽8</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">聽9</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">10</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">partial聽specialazation聽for聽int</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">11</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">12</span>聽<span style="COLOR: #000000">struct聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">></span></font> <font face="Courier New"> <span style="COLOR: #000000">聽{<br /></span> <span style="COLOR: #008080">13</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(</span><span style="COLOR: #0000ff">int</span></font> <font face="Courier New"> <span style="COLOR: #000000">聽t)聽{<br /></span> <span style="COLOR: #008080">14</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">1</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">15</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">16</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">17</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">18</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">partial聽specialazation聽for聽T*</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">19</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">20</span>聽<span style="COLOR: #000000">聽聽聽聽struct聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T聽</span><span style="COLOR: #000000">*></span></font> <font face="Courier New"> <span style="COLOR: #000000">聽{<br /></span> <span style="COLOR: #008080">21</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">聽</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽apply(T聽</span><span style="COLOR: #000000">*</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">22</span>聽<span style="COLOR: #000000">聽聽聽聽聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">2</span></font> <font face="Courier New"> <span style="COLOR: #000000">;<br /></span> <span style="COLOR: #008080">23</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">聽聽聽聽}<br /></span> <span style="COLOR: #008080">24</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">};<br /></span> <span style="COLOR: #008080">25</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">26</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">users,聽don't聽touch聽this!</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">27</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">template聽</span><span style="COLOR: #000000"><</span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">聽T</span><span style="COLOR: #000000">></span></font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">28</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">聽T聽</span><span style="COLOR: #000000">&</span></font> <font face="Courier New"> <span style="COLOR: #000000">t)聽{<br /></span> <span style="COLOR: #008080">29</span>聽<span style="COLOR: #000000">聽聽聽聽</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">聽FuncImpl</span><span style="COLOR: #000000"><</span><span style="COLOR: #000000">T</span><span style="COLOR: #000000">></span></font> <font face="Courier New"> <span style="COLOR: #000000">::apply(t);<br /></span> <span style="COLOR: #008080">30</span>聽</font> <font face="Courier New"> <span style="COLOR: #000000">}<br /></span> <span style="COLOR: #008080">31</span>聽</font> <span style="COLOR: #000000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">32</span>聽<span style="COLOR: #000000"></span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">聽i聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽</span><span style="COLOR: #000000">10</span></font> <font face="Courier New"> <span style="COLOR: #000000">,聽r;<br /></span> <span style="COLOR: #008080">33</span>聽<span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">c</span><span style="COLOR: #000000">'</span><span style="COLOR: #000000">);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽0</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">34</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">8</span><span style="COLOR: #000000">);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽1</span></font> <span style="COLOR: #008000"> <br /> </span> <font face="Courier New"> <span style="COLOR: #008080">35</span>聽<span style="COLOR: #008000"></span><span style="COLOR: #000000">r聽</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">聽func(</span><span style="COLOR: #000000">&</span><span style="COLOR: #000000">i);聽</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">r聽=聽2</span></font> </div> <img src ="http://m.shnenglu.com/ant/aggbug/31201.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.shnenglu.com/ant/" target="_blank">铓傝殎緇堢粨鑰?/a> 2007-08-30 13:55 <a href="http://m.shnenglu.com/ant/archive/2007/08/30/31201.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>浠嶹in32 API灝佽Thread綾籟1]http://m.shnenglu.com/ant/archive/2007/08/30/31190.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Thu, 30 Aug 2007 02:18:00 GMThttp://m.shnenglu.com/ant/archive/2007/08/30/31190.htmlhttp://m.shnenglu.com/ant/comments/31190.htmlhttp://m.shnenglu.com/ant/archive/2007/08/30/31190.html#Feedback8http://m.shnenglu.com/ant/comments/commentRss/31190.htmlhttp://m.shnenglu.com/ant/services/trackbacks/31190.html鍓嶅嚑澶╁湪瀛indows澶氱嚎紼嬬▼搴忚璁★紝鍙戠幇Win32 API鐢ㄨ搗鏉ョ‘瀹炰笉鎬庝箞鏂逛究錛岀壒鍒槸瀵逛簬C++紼嬪簭鍛樸備簬鏄疄鐜頒簡涓涓畝鍗曠殑灝佽錛屾妧鏈惈閲忓綋鐒朵笉楂橈紝涓嶈繃鐢ㄨ搗鏉ヨ繕鏄瘮杈冩柟渚跨殑銆傚鏋滀綘鐔熸?zhèn)塉ava錛屼綘浼?xì)鍙戠幇杩欎釜瀹炵幇鏈夌偣鍍廕ava鐨凾hread錛屽湪Java涓湁涓ょ鏂規(guī)硶鍙互鍒涘緩涓涓猅hread錛?br />
1.浠嶵hread綾葷戶鎵垮茍瀹炵幇run鏂規(guī)硶錛?/p>
1class聽MyThread聽extends 聽Thread聽{
2聽聽聽聽publicvoid
聽run()聽{
3
聽聽聽聽聽聽聽聽...
4
聽聽聽聽}聽
5
};
6

7//寮鍚嚎紼?/span>
8MyThread聽thread聽=new 聽MyThread;
9thread.start();

2.澹版槑涓涓被瀹炵幇Runnable鎺ュ彛騫跺疄鐜皉un鏂規(guī)硶錛?/p>

1class聽MyRunnable聽implements 聽Runnable聽{
2聽聽聽聽publicvoid
聽run()聽{
3
聽聽聽聽聽聽聽聽...
4
聽聽聽聽}
5
};
6

7//聽寮鍚嚎紼嬄?/span>
8MyThread聽thread聽=new聽MyThread(new 聽MyRunnable);
9thread.start();


鍏蜂綋瀹炵幇
Java鐨勫疄鐜版柟寮忚繕綆椾紭闆咃紝鎴戜滑涔熷彲浠ュ湪C++涓ā鎷燂紝鐢變簬綃囧箙鎵闄愶紝鍦ㄨ繖閲屾垜浠彧鍏蟲敞鍩烘湰鍔熻兘鐨勫疄鐜幫細(xì)
thread.h
聽1#ifndef聽THREAD_H
聽2
#define聽THREAD_H
聽3

聽4#include聽<windows.h>
聽5
聽6#define聽CLASS_UNCOPYABLE(classname)聽\
聽7聽聽聽聽private
:聽\
聽8聽聽聽聽classname(const聽classname&
);聽\
聽9聽聽聽聽classname&聽operator=(const聽classname&
);
10

11struct聽Runnable聽{
12聽聽聽聽virtual聽void聽run()聽=0
;
13聽聽聽聽virtual聽~
Runnable()聽{}
14
};
15

16class聽Thread聽:聽public聽Runnable聽{
17
聽聽聽聽CLASS_UNCOPYABLE(Thread)
18public
:
19聽聽聽聽explicit聽Thread(Runnable* target聽=0
);
20聽聽聽聽virtual聽~
Thread();
21聽聽聽聽virtual聽void
聽run()聽{}
22聽聽聽聽void
聽start();
23聽聽聽聽void
聽join();
24private
:
25聽聽聽聽static聽unsigned聽__stdcall聽threadProc(void*
param);
26private
:
27聽聽聽聽Runnable*
_target;
28
聽聽聽聽HANDLE聽_handle;
29
};
30

31#endif/*THREAD_H*/

鍦ㄤ笂闈㈢殑浠g爜涓垜浠厛瀹氫箟涓涓猂unnable綾伙紝騫朵負(fù)Runnable綾繪坊鍔犵函铏氬嚱鏁皉un錛岄渶瑕佺壒鍒敞鎰忕殑鏄疪unnable綾葷殑铏氭瀽鏋勫嚱鏁幫紝浠諱綍鎯寵鎴愪負(fù)鍩虹被鐨刢lass閮藉簲璇ュ0鏄庢瀽鏋勫嚱鏁頒負(fù)virtual銆?br />Thread綾葷洿鎺ョ戶鎵胯嚜Runnable錛孋LASS_UNCOPYABLE(Thread) 榪欎竴琛岀敤瀹忓0鏄嶵hread綾諱笉鍙嫹璐濓紝鍏蜂綋緇嗚妭璇風(fēng)湅 鑰冭檻鐢∕acro鏇挎崲Uncopyable 銆俿tart鎴愬憳鍑芥暟鍜孞ava涓殑涓鏍鳳紝鐢ㄦ潵寮鍚竴涓嚎紼嬶紝join鎴愬憳鍑芥暟鐢ㄦ潵闃誨褰撳墠綰跨▼鐩村埌寮鍚殑綰跨▼鎵ц瀹屾瘯銆倀hreadProc浣滀負(fù)闈欐佹垚鍛樺嚱鏁扮敤鏉ヤ紶閫掔粰_beginthreadex錛岃嚦浜庝負(fù)浠涔堢敤_beginthreadex浠f浛CreateThread璇烽棶Google銆傚垵姝ョ殑瀹炵幇閮芥瘮杈冪畝鍗曪紝鐩存帴璐翠笂浠g爜錛?br />thread.cpp
聽1#include聽"thread.h"
聽2#include聽<process.h>// for聽_beginthreadex
聽3
聽4Thread::Thread(Runnable* target聽/*=聽0*/)
聽5:_target(target
)
聽6,_handle(0
)聽{
聽7

聽8}
聽9

10Thread::~Thread()聽{
11聽聽聽聽if聽(_handle聽!=0
)
12
聽聽聽聽聽聽聽聽CloseHandle(_handle);
13聽聽聽聽if聽(_target聽!=0
)
14
聽聽聽聽聽聽聽聽delete聽_target;
15
}
16

17void聽Thread::start()聽{
18聽聽聽聽if聽(_handle聽!=0
)
19聽聽聽聽聽聽聽聽return
;
20
聽聽聽聽unsigned聽id;
21聽聽聽聽_handle聽=聽reinterpret_cast<HANDLE>
(
22聽聽聽聽聽聽聽聽_beginthreadex(0,聽0,聽threadProc,聽this,聽0,聽&
id)
23
聽聽聽聽聽聽聽聽);
24
}
25

26void聽Thread::join()聽{
27聽聽聽聽if(_handle聽!=0
)聽{
28
聽聽聽聽聽聽聽聽WaitForSingleObject(_handle,聽INFINITE);
29
聽聽聽聽聽聽聽聽CloseHandle(_handle);
30聽聽聽聽聽聽聽聽_handle聽=0
;
31
聽聽聽聽}
32
}
33

34unsigned聽__stdcall聽Thread::threadProc(void* param)聽{
35聽聽聽聽Thread* p聽=聽static_cast<Thread*>
(param);
36聽聽聽聽if聽(p->_target聽!=0
)
37聽聽聽聽聽聽聽聽p->_target->
run();
38聽聽聽聽else

39聽聽聽聽聽聽聽聽p->run();
40聽聽聽聽return0
;
41}

涓嬮潰鏄祴璇曚唬鐮侊細(xì)
test.cpp
聽1#include聽"thread.h"
聽2#include聽<iostream>
聽3
聽4using聽namespace聽std;
聽5

聽6//絎竴縐嶆柟娉曪紝浠嶵hread綾葷戶鎵?/span>
聽7struct聽MyThread聽:聽public聽Thread聽{
聽8聽聽聽聽virtual聽void
聽run()聽{
聽9聽聽聽聽聽聽聽聽for聽(int聽i聽=0;聽i聽<5;聽++
i)聽{
10聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<"MyThread聽Running..."<<聽i聽<<
聽endl;
11聽聽聽聽聽聽聽聽聽聽聽聽Sleep(100
);
12
聽聽聽聽聽聽聽聽}
13
聽聽聽聽}
14
};
15

16//絎簩縐嶆柟娉曪紝鈥滃疄鐜扳漅unnable鎺ュ彛
17struct聽MyRunnable聽:聽public聽Runnable聽{
18聽聽聽聽virtual聽void
聽run()聽{
19聽聽聽聽聽聽聽聽for聽(int聽i聽=0;聽i聽<5;聽++
i)聽{
20聽聽聽聽聽聽聽聽聽聽聽聽cout聽<<"MyRunnable聽Running..."<<聽i聽<<
聽endl;
21聽聽聽聽聽聽聽聽聽聽聽聽Sleep(300
);
22
聽聽聽聽聽聽聽聽}
23
聽聽聽聽}
24
};
25

26int聽main()聽{
27

28聽聽聽聽MyThread聽thread1;
29聽聽聽聽Thread聽thread2(new
聽MyRunnable());
30
聽聽聽聽thread1.start();
31
聽聽聽聽thread2.start();
32
聽聽聽聽thread1.join();
33
聽聽聽聽thread2.join();
34

35聽聽聽聽return0;
36}

鍙兘鐨勮繍琛岀粨鏋滐細(xì)
MyThread Running...0
MyRunnable Running...0
MyThread Running...1
MyThread Running...2
MyRunnable Running...1
MyThread Running...3
MyThread Running...4
MyRunnable Running...2
MyRunnable Running...3
MyRunnable Running...4


濡傛灉浣犵啛鎮(zhèn)塨oost搴擄紝浣犱細(xì)鐭ラ亾boost搴撶殑Thread鏋勯犲嚱鏁板彲浠ユ帴鍙楁櫘閫氬嚱鏁板拰鍑芥暟瀵硅薄浣滀負(fù)鍙傛暟錛屽鏋滀綘瑙夊緱浠嶵hread綾葷戶鎵挎垨鑰呭疄鐜癛unnable鎺ュ彛榪樹笉澶熺畝媧侊紝涓嬩竴綃囦細(xì)鏈変竴涓瘮杈冨ソ鐨勬敼榪涖?

]]>
鑰冭檻鐢∕acro鏇挎崲Uncopyablehttp://m.shnenglu.com/ant/archive/2007/08/27/30908.html铓傝殎緇堢粨鑰?/dc:creator>铓傝殎緇堢粨鑰?/author>Mon, 27 Aug 2007 00:11:00 GMThttp://m.shnenglu.com/ant/archive/2007/08/27/30908.htmlhttp://m.shnenglu.com/ant/comments/30908.htmlhttp://m.shnenglu.com/ant/archive/2007/08/27/30908.html#Feedback2http://m.shnenglu.com/ant/comments/commentRss/30908.htmlhttp://m.shnenglu.com/ant/services/trackbacks/30908.html1. 灝嗚繖涓被鐨刢opy鏋勯犲嚱鏁板拰copy assignment鎿嶄綔絎﹂兘澹版槑涓簆rivate鑰屼笖娌℃湁瀹氫箟銆傜ず渚嬩唬鐮佸涓嬶細(xì)
1class聽SomeClass聽{
2
聽聽聽聽...
3private
:
4聽聽聽聽//鍙湁澹版槑

5聽聽聽聽SomeClass(const聽SomeClass&);
6聽聽聽聽SomeClass&聽operator=(const聽SomeClass&
);
7};

2. 涓撻棬璁捐涓涓負(fù)浜嗛樆姝opying鍔ㄤ綔鑰岃璁$殑base class錛岃繖涓猙ase class鐩稿綋綆鍗曪細(xì)
1Uncopyable聽{
2protected:聽//鍏佽derived瀵硅薄鏋勯犲拰鏋愭瀯

3聽聽聽聽Uncopyable()聽{}
4聽聽聽聽~
Uncopyable()聽{}
5private:聽//闃繪copying

6聽聽聽聽Uncopyable(const聽Uncopyable&);
7聽聽聽聽Uncopyable&聽operator=(const聽Uncopyable&
);
8};

涓轟簡闃繪鏌愪釜瀵硅薄濡係omeClass琚玞opying錛屾垜浠敮涓闇瑕佸仛鐨勫氨鏄戶鎵胯嚜Uncopyable錛?br />
1SomeClass聽:聽private聽Uncopyable聽{
2
聽聽聽聽...
3};

絎?縐嶆柟妗堣矊浼煎緢瀹岀編錛屽湪澶у鏁版儏鍐典笅涔熷伐浣滅殑寰堝ソ錛岀敋鑷砨oost搴撲篃騫挎硾閲囩敤浜?boost搴撲腑鐨勫彨noncopyable)銆傚綋鐒朵簡錛屽洜涓篣ncopyable涓嶅惈浠諱綍鏁版嵁錛岀鍚圗BO(empty base class optimization錛岀┖鐧藉熀綾繪渶浼樺寲)璧勬牸錛屼絾鐢變簬瀹冩繪槸鎵紨base class錛屽洜姝ゆ湁鍙兘瀵艱嚧澶氶噸緇ф壙錛岃孍BO涓鑸彧鍦ㄥ崟緇ф壙涓嬫墠鍙錛岀紪璇戝櫒閫氬父涓嶄細(xì)瀵規(guī)湁澶氫釜base class鐨刣erived classes鏂借EBO銆?br />
濡傛灉浣犱笉鎯沖鑷村閲嶇戶鎵匡紝涓嶇鏄粠鎬ц兘涓婅繕鏄功鍐欎唬鐮佷笂錛屾瘮濡係omeClass緇ф壙鑷煇涓猙ase class錛屼綘鍙兘浼?xì)鍐欏涓嬩唬鐮佸Q?br />
1class聽SomeClass聽:聽public聽BaseClass,聽private聽Uncopyable聽{
2
聽聽聽聽...
3};

榪欒浜哄緢涓嶇埥錛岃鏄綘璺熸垜涓鏍鋒噿錛屼綘鑲畾涔熶笉浼?xì)鎰挎剰閲囩敤绗竴縐嶆柟妗堬紝鍥犱負(fù)閭f剰鍛崇潃浣犱細(xì)鍦ㄦ瘡涓涓笉鎯寵copying鐨刢lass閲岄兘鍔犱笂閭d笁琛屼唬鐮併?br />
濡傛灉鐢∕acro灝卞緢綆鍗曚簡錛屽嵆鐏墊椿鍙堟柟渚匡細(xì)
1#define聽CLASS_UNCOPYABLE(classname)聽\
2聽聽聽聽private
:聽\
3聽聽聽聽classname(const聽classname&
);聽\
4聽聽聽聽classname&聽operator=(const聽classname&);

鐜板湪SomeClass鍙渶鍐欐垚榪欐牱錛?br />
1class聽SomeClass聽{
2
聽聽聽聽CLASS_UNCOPYABLE(SomeClass)
3
聽聽聽聽...
4};

涓嶄粎鐪佷簨鑰屼笖鐏墊椿錛侊紒錛?img src ="http://m.shnenglu.com/ant/aggbug/30908.html" width = "1" height = "1" />

]]>
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美成人午夜77777| 国产精品婷婷| 免费人成网站在线观看欧美高清| 欧美视频在线一区二区三区| 精品成人国产在线观看男人呻吟| a4yy欧美一区二区三区| 欧美黑人一区二区三区| 久久国产精品一区二区三区四区| 国产欧美日韩视频一区二区三区 | 亚洲电影在线免费观看| 欧美在线视频一区二区| 亚洲一级二级在线| 国产精品久久午夜夜伦鲁鲁| 亚洲欧美美女| 一个色综合av| 亚洲精品专区| 欧美三级免费| 亚洲与欧洲av电影| 亚洲视频在线二区| 国产精品视频yy9099| 欧美在线一级va免费观看| 性欧美超级视频| 一区二区在线不卡| 亚洲高清资源综合久久精品| 久久在线播放| 日韩一级片网址| 一本色道久久88综合亚洲精品ⅰ| 国产精品乱码一区二三区小蝌蚪| 久久国产精品久久久久久久久久| 欧美一区在线直播| 亚洲国产精选| 在线亚洲国产精品网站| 国产亚洲精品美女| 亚洲第一毛片| 国产精品毛片a∨一区二区三区| 欧美一区二区三区播放老司机| 久久不见久久见免费视频1| 亚洲国产美女| 亚洲午夜三级在线| 亚洲电影免费观看高清完整版| 亚洲精品国精品久久99热一| 国产情侣久久| 亚洲国产精品日韩| 国产精品久久久久久福利一牛影视| 欧美在线一区二区| 欧美成人在线影院| 欧美亚洲免费| 欧美激情1区2区| 久久国产欧美精品| 欧美日韩免费看| 噜噜噜躁狠狠躁狠狠精品视频 | 欧美精品在线极品| 亚洲欧美日韩天堂| 欧美va天堂在线| 欧美在线一区二区三区| 欧美韩日一区| 久久久久综合网| 国产精品igao视频网网址不卡日韩| 久久野战av| 国产精品初高中精品久久| 欧美成人免费一级人片100| 国产精品卡一卡二卡三| 亚洲激情一区二区| 一区二区亚洲精品| 午夜在线一区二区| 亚洲一区二区精品在线| 蜜臀91精品一区二区三区| 欧美一区二区精品久久911| 欧美激情亚洲另类| 免费亚洲视频| 激情综合在线| 欧美伊久线香蕉线新在线| 亚洲在线视频网站| 欧美日韩国产综合久久| 欧美激情精品| 亚洲高清不卡av| 久久精品91久久久久久再现| 国产欧美在线视频| 亚洲女女女同性video| 亚洲一二三区精品| 欧美日韩色婷婷| 亚洲国产精品热久久| 亚洲国产第一| 久热成人在线视频| 欧美电影免费观看高清| 在线观看一区视频| 久久一区二区视频| 欧美成人自拍视频| 亚洲人体一区| 欧美精品18videos性欧美| 亚洲激情欧美| 中日韩视频在线观看| 欧美日韩中文字幕日韩欧美| 亚洲激情视频在线播放| 日韩午夜av在线| 欧美日韩大片| 一区二区三区日韩精品视频| 亚洲欧美日韩国产中文| 国产精品久久久久一区二区三区共 | 欧美在线地址| 国产精品亚洲欧美| 亚洲欧美久久久| 久久久蜜桃一区二区人| 黄色另类av| 美女国内精品自产拍在线播放| 欧美本精品男人aⅴ天堂| 亚洲日本欧美| 欧美日韩另类综合| 亚洲天堂免费在线观看视频| 久久久777| 亚洲国产日韩欧美在线动漫| 欧美精品情趣视频| 亚洲性色视频| 免费国产一区二区| 一区二区三区 在线观看视| 国产精品成人一区| 久久电影一区| 亚洲国语精品自产拍在线观看| 亚洲视频一区二区| 国产日韩一区二区三区| 美女爽到呻吟久久久久| 99精品福利视频| 久久久99免费视频| 亚洲精品视频在线观看网站| 国产精品免费一区豆花| 六月婷婷久久| 午夜精品久久久久久久99黑人| 免费成人在线观看视频| 一区二区冒白浆视频| 黑人一区二区| 欧美日韩一区二区免费视频| 久久精品视频免费播放| 日韩视频一区二区在线观看 | 亚洲淫性视频| 在线观看成人一级片| 欧美视频福利| 久久久久国色av免费看影院| 日韩网站在线看片你懂的| 亚洲国产精品嫩草影院| 国产精品萝li| 欧美成人精品影院| 欧美一区二区三区在线视频| 亚洲精品九九| 欧美成年人视频| 久久久av毛片精品| 模特精品在线| 久久gogo国模裸体人体| 亚洲美女色禁图| 欧美www在线| 久久久久**毛片大全| 亚洲免费中文| 亚洲最新在线| 亚洲日本一区二区| 有坂深雪在线一区| 国产日韩欧美视频在线| 国产精品国产三级国产专播精品人| 免费高清在线一区| 久久国内精品自在自线400部| 这里只有视频精品| 亚洲黄色一区| 亚洲成色777777女色窝| 乱码第一页成人| 久久久久se| 久久精品人人做人人综合 | 免费在线观看日韩欧美| 欧美自拍偷拍| 性欧美长视频| 亚洲欧美日韩综合国产aⅴ| 艳妇臀荡乳欲伦亚洲一区| 亚洲欧洲精品成人久久奇米网| 在线日韩av永久免费观看| 国模精品娜娜一二三区| 国产欧美婷婷中文| 国产午夜精品在线| 国产一区二区在线免费观看| 国产精品综合网站| 国产精品资源在线观看| 国产精品麻豆成人av电影艾秋| 国产精品va在线播放| 国产精品美女| 国产一区日韩一区| 激情六月婷婷综合| 亚洲高清在线视频| 最新亚洲一区| 亚洲香蕉网站| 性欧美长视频| 久久香蕉国产线看观看网| 欧美成人亚洲成人日韩成人| 免费不卡在线视频| 亚洲电影观看| 宅男在线国产精品| 欧美夜福利tv在线| 久久久蜜臀国产一区二区| 免费一区二区三区| 欧美色视频在线| 国产日产亚洲精品| 亚洲第一在线| 亚洲一区免费观看| 久久精品在这里| 亚洲电影一级黄|