﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-CPP-FanClub-文章分类-C plus plus</title><link>http://www.cppblog.com/gulian/category/5064.html</link><description>I think and therefore I am.</description><language>zh-cn</language><lastBuildDate>Sat, 31 May 2008 13:56:00 GMT</lastBuildDate><pubDate>Sat, 31 May 2008 13:56:00 GMT</pubDate><ttl>60</ttl><item><title>如何在main函數返回0前，輸入一個任意鍵結束程式?</title><link>http://www.cppblog.com/gulian/articles/31354.html</link><dc:creator>林政夆</dc:creator><author>林政夆</author><pubDate>Sat, 01 Sep 2007 04:52:00 GMT</pubDate><guid>http://www.cppblog.com/gulian/articles/31354.html</guid><wfw:comment>http://www.cppblog.com/gulian/comments/31354.html</wfw:comment><comments>http://www.cppblog.com/gulian/articles/31354.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/gulian/comments/commentRss/31354.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/gulian/services/trackbacks/31354.html</trackback:ping><description><![CDATA[<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%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">iostream</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cstdlib</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><br>#ifndef&nbsp;_CPAUSE_<br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;_CPAUSE_</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;CPause {<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">static</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;system()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::system(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">pause</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;CPause()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::atexit(</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">CPause::system);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}m_pause;<br><br></span><span style="COLOR: #0000ff">#endif</span><span style="COLOR: #000000">&nbsp;</span></div>
<br>程式相當的簡單，當<span style="COLOR: #000000">CPause類別的建構式喚起時，就會呼叫標準函式庫中</span><span style="COLOR: #000000">atexit()並且執行</span><span style="COLOR: #000000">system("pause"</span><span style="COLOR: #000000"></span><span style="COLOR: #000000">)。這樣的話在客戶端程式結束前會呼叫</span><span style="COLOR: #000000"></span><span style="COLOR: #000000">system(</span><span style="COLOR: #000000"></span><span style="COLOR: #000000">"pause"</span><span style="COLOR: #000000"></span><span style="COLOR: #000000"></span><span style="COLOR: #000000">)，而</span><span style="COLOR: #000000">且m_pause為靜態的全域物件，也就是說當程式執行時該物件會第一個實例化，最後一個被解構銷毀。<br><br>註記：<br></span><span style="COLOR: #000000">有關於atexit說明如下</span><br><span style="COLOR: #000000">http://www.cplusplus.com/reference/clibrary/cstdlib/atexit.html</span><span style="FONT-SIZE: xx-large"><span style="FONT-WEIGHT: bold"></span></span> 
<img src ="http://www.cppblog.com/gulian/aggbug/31354.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/gulian/" target="_blank">林政夆</a> 2007-09-01 12:52 <a href="http://www.cppblog.com/gulian/articles/31354.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>