﻿<?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++博客-C++之竹-随笔分类-设计模式</title><link>http://www.cppblog.com/qingbizhu/category/19096.html</link><description>无论是太阳下，还是风雨中，都要成长！</description><language>zh-cn</language><lastBuildDate>Wed, 11 Apr 2012 17:45:47 GMT</lastBuildDate><pubDate>Wed, 11 Apr 2012 17:45:47 GMT</pubDate><ttl>60</ttl><item><title>Singleton模式——C++应用（四） </title><link>http://www.cppblog.com/qingbizhu/archive/2012/03/14/167826.html</link><dc:creator>青碧竹</dc:creator><author>青碧竹</author><pubDate>Tue, 13 Mar 2012 17:07:00 GMT</pubDate><guid>http://www.cppblog.com/qingbizhu/archive/2012/03/14/167826.html</guid><wfw:comment>http://www.cppblog.com/qingbizhu/comments/167826.html</wfw:comment><comments>http://www.cppblog.com/qingbizhu/archive/2012/03/14/167826.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qingbizhu/comments/commentRss/167826.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qingbizhu/services/trackbacks/167826.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;在前面的例子中，我们看到：采用 new 来为单件对象分配空间，如果采用手动调用 delete 或封装了 delete 的 Release 操作，一旦遇到全局对象的析构有调用单件对象，就会使得无法在代码中找到适合释放单件对象的时机。那么，是否可以让系统来自动选择时机，调用释放呢？如果可以，又该怎么在代码中构建单件对象的自动释放机制呢? 对这两个问题，在进行了一番思考和尝试后，终于找到了答...&nbsp;&nbsp;<a href='http://www.cppblog.com/qingbizhu/archive/2012/03/14/167826.html'>阅读全文</a><img src ="http://www.cppblog.com/qingbizhu/aggbug/167826.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qingbizhu/" target="_blank">青碧竹</a> 2012-03-14 01:07 <a href="http://www.cppblog.com/qingbizhu/archive/2012/03/14/167826.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Singleton模式——C++应用（三）</title><link>http://www.cppblog.com/qingbizhu/archive/2012/03/13/167745.html</link><dc:creator>青碧竹</dc:creator><author>青碧竹</author><pubDate>Mon, 12 Mar 2012 16:55:00 GMT</pubDate><guid>http://www.cppblog.com/qingbizhu/archive/2012/03/13/167745.html</guid><wfw:comment>http://www.cppblog.com/qingbizhu/comments/167745.html</wfw:comment><comments>http://www.cppblog.com/qingbizhu/archive/2012/03/13/167745.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qingbizhu/comments/commentRss/167745.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qingbizhu/services/trackbacks/167745.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 前面对C++的Singleton模式的探讨还都是针对通过静态变量来创建对象。但学习嘛，多走点总不是坏事。接下来就来看看通过 new 来创建单件对象的单件类设计。既然是用 new 来创建了，那自然就不能忽略需要用 delete 来释放。好了，先来看看代码：Code highlighting produced by Actipro CodeHighlighter (freeware)htt...&nbsp;&nbsp;<a href='http://www.cppblog.com/qingbizhu/archive/2012/03/13/167745.html'>阅读全文</a><img src ="http://www.cppblog.com/qingbizhu/aggbug/167745.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qingbizhu/" target="_blank">青碧竹</a> 2012-03-13 00:55 <a href="http://www.cppblog.com/qingbizhu/archive/2012/03/13/167745.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Singleton模式——C++应用（二）</title><link>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167682.html</link><dc:creator>青碧竹</dc:creator><author>青碧竹</author><pubDate>Sun, 11 Mar 2012 18:08:00 GMT</pubDate><guid>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167682.html</guid><wfw:comment>http://www.cppblog.com/qingbizhu/comments/167682.html</wfw:comment><comments>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167682.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qingbizhu/comments/commentRss/167682.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qingbizhu/services/trackbacks/167682.html</trackback:ping><description><![CDATA[<p>看了通过静态成员实现的惰性单件，虽说构件简单且线程安全，但在被全局变量（包括静态全局变量）的构造或析构调用时，会存在着那样的缺陷毕竟还是很让人不爽。那么，是否存在保持上面的优点同时剔除了上面的缺陷的C++单件呢？</p>
<p>&nbsp;</p>
<p>我们知道，定义于函数内部的静态局部变量，有着</p>
<ul><li>定义的实时性，即只在函数第一次被调用时才被定义</li><li>线程安全性</li><li>生命期直到程序结束</li></ul>
<p>的特点。那么，通过这个局部静态变量来创建的单件又会是什么样呢？贴上我的Demo代码，这就来看看吧。</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Singleton&nbsp;demo_1:&nbsp;Singleton&nbsp;instance&nbsp;is&nbsp;a&nbsp;local&nbsp;static&nbsp;varible&nbsp;of&nbsp;class.</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;&nbsp;2</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">Windows.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;A&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;7</span><span style="color: #000000"><img id="Codehighlighter1_128_602_Open_Image" onclick="this.style.display='none'; Codehighlighter1_128_602_Open_Text.style.display='none'; Codehighlighter1_128_602_Closed_Image.style.display='inline'; Codehighlighter1_128_602_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_128_602_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_128_602_Closed_Text.style.display='none'; Codehighlighter1_128_602_Open_Image.style.display='inline'; Codehighlighter1_128_602_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_128_602_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_128_602_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;A</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;GetInstance();<br /></span><span style="color: #008080">&nbsp;10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;12</span><span style="color: #000000"><img id="Codehighlighter1_208_286_Open_Image" onclick="this.style.display='none'; Codehighlighter1_208_286_Open_Text.style.display='none'; Codehighlighter1_208_286_Closed_Image.style.display='inline'; Codehighlighter1_208_286_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_208_286_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_208_286_Closed_Text.style.display='none'; Codehighlighter1_208_286_Open_Image.style.display='inline'; Codehighlighter1_208_286_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;A()&nbsp;:&nbsp;m_nStat1(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">),&nbsp;m_nStat2(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_208_286_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_208_286_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;A</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;17</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;A(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;A</span><span style="color: #000000">&amp;</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;20</span><span style="color: #000000"><img id="Codehighlighter1_317_394_Open_Image" onclick="this.style.display='none'; Codehighlighter1_317_394_Open_Text.style.display='none'; Codehighlighter1_317_394_Closed_Image.style.display='inline'; Codehighlighter1_317_394_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_317_394_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_317_394_Closed_Text.style.display='none'; Codehighlighter1_317_394_Open_Image.style.display='inline'; Codehighlighter1_317_394_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">A()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_317_394_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_317_394_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;A</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;24</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;25</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;26</span><span style="color: #000000"><img id="Codehighlighter1_408_560_Open_Image" onclick="this.style.display='none'; Codehighlighter1_408_560_Open_Text.style.display='none'; Codehighlighter1_408_560_Closed_Image.style.display='inline'; Codehighlighter1_408_560_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_408_560_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_408_560_Closed_Text.style.display='none'; Codehighlighter1_408_560_Open_Image.style.display='inline'; Codehighlighter1_408_560_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Do()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_408_560_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_408_560_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat1;<br /></span><span style="color: #008080">&nbsp;28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat2;<br /></span><span style="color: #008080">&nbsp;29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Called&nbsp;Do()&nbsp;by&nbsp;object&nbsp;of&nbsp;A.&nbsp;[</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;31</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">]</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;32</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;33</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;34</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;35</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;36</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat1;<br /></span><span style="color: #008080">&nbsp;37</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat2;<br /></span><span style="color: #008080">&nbsp;38</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;39</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;40</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;B<br /></span><span style="color: #008080">&nbsp;41</span><span style="color: #000000"><img id="Codehighlighter1_614_844_Open_Image" onclick="this.style.display='none'; Codehighlighter1_614_844_Open_Text.style.display='none'; Codehighlighter1_614_844_Closed_Image.style.display='inline'; Codehighlighter1_614_844_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_614_844_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_614_844_Closed_Text.style.display='none'; Codehighlighter1_614_844_Open_Image.style.display='inline'; Codehighlighter1_614_844_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_614_844_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_614_844_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;42</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;43</span><span style="color: #000000"><img id="Codehighlighter1_649_731_Open_Image" onclick="this.style.display='none'; Codehighlighter1_649_731_Open_Text.style.display='none'; Codehighlighter1_649_731_Closed_Image.style.display='inline'; Codehighlighter1_649_731_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_649_731_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_649_731_Closed_Text.style.display='none'; Codehighlighter1_649_731_Open_Image.style.display='inline'; Codehighlighter1_649_731_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;B(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nID)&nbsp;:&nbsp;m_nID(nID)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_649_731_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_649_731_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;44</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;B:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nID&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;45</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">&nbsp;46</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;47</span><span style="color: #000000"><img id="Codehighlighter1_739_820_Open_Image" onclick="this.style.display='none'; Codehighlighter1_739_820_Open_Text.style.display='none'; Codehighlighter1_739_820_Closed_Image.style.display='inline'; Codehighlighter1_739_820_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_739_820_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_739_820_Closed_Text.style.display='none'; Codehighlighter1_739_820_Open_Image.style.display='inline'; Codehighlighter1_739_820_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">B()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_739_820_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_739_820_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;48</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;B:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nID&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;49</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">&nbsp;50</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;51</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;52</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;53</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nID;<br /></span><span style="color: #008080">&nbsp;54</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;55</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;56</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;C<br /></span><span style="color: #008080">&nbsp;57</span><span style="color: #000000"><img id="Codehighlighter1_856_1216_Open_Image" onclick="this.style.display='none'; Codehighlighter1_856_1216_Open_Text.style.display='none'; Codehighlighter1_856_1216_Closed_Image.style.display='inline'; Codehighlighter1_856_1216_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_856_1216_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_856_1216_Closed_Text.style.display='none'; Codehighlighter1_856_1216_Open_Image.style.display='inline'; Codehighlighter1_856_1216_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_856_1216_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_856_1216_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;58</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;59</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;C</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;GetInstance();<br /></span><span style="color: #008080">&nbsp;60</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;61</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;62</span><span style="color: #000000"><img id="Codehighlighter1_921_982_Open_Image" onclick="this.style.display='none'; Codehighlighter1_921_982_Open_Text.style.display='none'; Codehighlighter1_921_982_Closed_Image.style.display='inline'; Codehighlighter1_921_982_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_921_982_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_921_982_Closed_Text.style.display='none'; Codehighlighter1_921_982_Open_Image.style.display='inline'; Codehighlighter1_921_982_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;C()&nbsp;:&nbsp;m_nStat(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_921_982_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_921_982_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;63</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;C</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;64</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;65</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;66</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;C(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;C</span><span style="color: #000000">&amp;</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;67</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;68</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;69</span><span style="color: #000000"><img id="Codehighlighter1_1013_1073_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1013_1073_Open_Text.style.display='none'; Codehighlighter1_1013_1073_Closed_Image.style.display='inline'; Codehighlighter1_1013_1073_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1013_1073_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1013_1073_Closed_Text.style.display='none'; Codehighlighter1_1013_1073_Open_Image.style.display='inline'; Codehighlighter1_1013_1073_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">C()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1013_1073_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1013_1073_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;70</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;C</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;71</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;72</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;73</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;74</span><span style="color: #000000"><img id="Codehighlighter1_1087_1190_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1087_1190_Open_Text.style.display='none'; Codehighlighter1_1087_1190_Closed_Image.style.display='inline'; Codehighlighter1_1087_1190_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1087_1190_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1087_1190_Closed_Text.style.display='none'; Codehighlighter1_1087_1190_Open_Image.style.display='inline'; Codehighlighter1_1087_1190_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Do()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1087_1190_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1087_1190_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;75</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat;<br /></span><span style="color: #008080">&nbsp;76</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Called&nbsp;Do()&nbsp;by&nbsp;object&nbsp;of&nbsp;C.&nbsp;[</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;77</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">]</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;78</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;79</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;80</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;81</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;82</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat;<br /></span><span style="color: #008080">&nbsp;83</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;84</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;85</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;B&nbsp;gs_B0(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;86</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />B&nbsp;g_B1(</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;87</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;88</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />A</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;A::GetInstance()<br /></span><span style="color: #008080">&nbsp;89</span><span style="color: #000000"><img id="Codehighlighter1_1271_1317_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1271_1317_Open_Text.style.display='none'; Codehighlighter1_1271_1317_Closed_Image.style.display='inline'; Codehighlighter1_1271_1317_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1271_1317_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1271_1317_Closed_Text.style.display='none'; Codehighlighter1_1271_1317_Open_Image.style.display='inline'; Codehighlighter1_1271_1317_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1271_1317_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1271_1317_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;90</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;A&nbsp;s_instance;<br /></span><span style="color: #008080">&nbsp;91</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #008080">&nbsp;92</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;s_instance;<br /></span><span style="color: #008080">&nbsp;93</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;94</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;95</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />C</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;C::GetInstance()<br /></span><span style="color: #008080">&nbsp;96</span><span style="color: #000000"><img id="Codehighlighter1_1340_1385_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1340_1385_Open_Text.style.display='none'; Codehighlighter1_1340_1385_Closed_Image.style.display='inline'; Codehighlighter1_1340_1385_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1340_1385_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1340_1385_Closed_Text.style.display='none'; Codehighlighter1_1340_1385_Open_Image.style.display='inline'; Codehighlighter1_1340_1385_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1340_1385_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1340_1385_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;97</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;C&nbsp;s_instance;<br /></span><span style="color: #008080">&nbsp;98</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;99</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;s_instance;<br /></span><span style="color: #008080">100</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">101</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">102</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;B&nbsp;gs_B2(</span><span style="color: #000000">2</span><span style="color: #000000">);<br /></span><span style="color: #008080">103</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />B&nbsp;g_B3(</span><span style="color: #000000">3</span><span style="color: #000000">);<br /></span><span style="color: #008080">104</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">105</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;argv[])<br /></span><span style="color: #008080">106</span><span style="color: #000000"><img id="Codehighlighter1_1453_1574_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1453_1574_Open_Text.style.display='none'; Codehighlighter1_1453_1574_Closed_Image.style.display='inline'; Codehighlighter1_1453_1574_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1453_1574_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1453_1574_Closed_Text.style.display='none'; Codehighlighter1_1453_1574_Open_Image.style.display='inline'; Codehighlighter1_1453_1574_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1453_1574_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1453_1574_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">107</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Enter&nbsp;main</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">108</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">109</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;C::GetInstance().Do();<br /></span><span style="color: #008080">110</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">111</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">112</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">113</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">114</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span></div>
<p>&nbsp;接下来，自然就是执行结果：</p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/qingbizhu/Ret_1.PNG" width="396" height="391" /></p>
<p><img border="0" src="http://www.cppblog.com/Emoticons/QQ/icon14.gif" width="20" height="21"  alt="" />哟吼~，这不正是自己想要的结果么，Perfect！！</p>
<p>构建简单，创建具有实时性（在且只在第一次用到时创建），线程安全，可以被全局变量的构造和析构正常调用，就是它了，极力推荐！O(&#8745;_&#8745;)O~</p>
<p>&nbsp;</p>  <img src ="http://www.cppblog.com/qingbizhu/aggbug/167682.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qingbizhu/" target="_blank">青碧竹</a> 2012-03-12 02:08 <a href="http://www.cppblog.com/qingbizhu/archive/2012/03/12/167682.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Singleton模式——C++应用（一）</title><link>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167681.html</link><dc:creator>青碧竹</dc:creator><author>青碧竹</author><pubDate>Sun, 11 Mar 2012 18:04:00 GMT</pubDate><guid>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167681.html</guid><wfw:comment>http://www.cppblog.com/qingbizhu/comments/167681.html</wfw:comment><comments>http://www.cppblog.com/qingbizhu/archive/2012/03/12/167681.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/qingbizhu/comments/commentRss/167681.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/qingbizhu/services/trackbacks/167681.html</trackback:ping><description><![CDATA[<div>&nbsp;&nbsp;&nbsp;&nbsp; 前天，在看了《剑指Offer》中关于Singleton模式的一段内容后，就按耐不住对其在C++上完美实现的思考。书中这一讲是针对C#的实现来讲解的，因为C#的垃圾回收机制，其中那些个显眼的[new]已无需程序员去手动释放。但是，C++上确并不存在垃圾回收机制，程序员得手动[delete] [new]出来的内容，这就有了以下两问：</div>
<ol><li>何时释放通过new所创建的Singleton对象？</li><li>如果不通过new、malloc等操作，是否能做到在第一次使用时才创建Singleton对象。</li></ol>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 在针对以上两个问题做了一番思考和尝试之后，终于有了成果。现呈现出来，供大家参考，也希望有更好妙招的朋友能不吝赐教。碍于连代码带文字，篇幅较长，所以我将分为四篇来讲。那么，开始咯！^_^</p>
<p>&nbsp;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; 一开始，我先拿使用静态成员变量的来实现的惰性单件来进行尝试，写了个Demo，一运行，O(&#8745;_&#8745;)O哈哈~，还真有意外的收获呢！嗯~，抓紧时间，先把我的Demo上来给大家瞧瞧！~</p>
<div style="border-bottom: #cccccc 1px solid; border-left: #cccccc 1px solid; padding-bottom: 4px; background-color: #eeeeee; padding-left: 4px; width: 98%; padding-right: 5px; font-size: 13px; word-break: break-all; border-top: #cccccc 1px solid; border-right: #cccccc 1px solid; padding-top: 4px"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008080">&nbsp;&nbsp;1</span><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><span style="color: #008000">//</span><span style="color: #008000">&nbsp;Singleton&nbsp;demo_0:&nbsp;Singleton&nbsp;instance&nbsp;is&nbsp;a&nbsp;static&nbsp;member&nbsp;of&nbsp;class.</span><span style="color: #008000"><br /></span><span style="color: #008080">&nbsp;&nbsp;2</span><span style="color: #008000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;3</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">Windows.h</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;4</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />#include&nbsp;</span><span style="color: #000000">&lt;</span><span style="color: #000000">iostream</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;&nbsp;5</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;&nbsp;6</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;A&nbsp;<br /></span><span style="color: #008080">&nbsp;&nbsp;7</span><span style="color: #000000"><img id="Codehighlighter1_121_654_Open_Image" onclick="this.style.display='none'; Codehighlighter1_121_654_Open_Text.style.display='none'; Codehighlighter1_121_654_Closed_Image.style.display='inline'; Codehighlighter1_121_654_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_121_654_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_121_654_Closed_Text.style.display='none'; Codehighlighter1_121_654_Open_Image.style.display='inline'; Codehighlighter1_121_654_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_121_654_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_121_654_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;&nbsp;8</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;&nbsp;9</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;A&nbsp;ms_instance;<br /></span><span style="color: #008080">&nbsp;10</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;11</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;12</span><span style="color: #000000"><img id="Codehighlighter1_189_214_Open_Image" onclick="this.style.display='none'; Codehighlighter1_189_214_Open_Text.style.display='none'; Codehighlighter1_189_214_Closed_Image.style.display='inline'; Codehighlighter1_189_214_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_189_214_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_189_214_Closed_Text.style.display='none'; Codehighlighter1_189_214_Open_Image.style.display='inline'; Codehighlighter1_189_214_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;A</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;GetInstance()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_189_214_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_189_214_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;13</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;ms_instance;<br /></span><span style="color: #008080">&nbsp;14</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;15</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;16</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;17</span><span style="color: #000000"><img id="Codehighlighter1_260_338_Open_Image" onclick="this.style.display='none'; Codehighlighter1_260_338_Open_Text.style.display='none'; Codehighlighter1_260_338_Closed_Image.style.display='inline'; Codehighlighter1_260_338_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_260_338_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_260_338_Closed_Text.style.display='none'; Codehighlighter1_260_338_Open_Image.style.display='inline'; Codehighlighter1_260_338_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;A()&nbsp;:&nbsp;m_nStat1(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">),&nbsp;m_nStat2(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_260_338_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_260_338_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;18</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;19</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;A</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;20</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;21</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;22</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;A(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;A</span><span style="color: #000000">&amp;</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;23</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;24</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;25</span><span style="color: #000000"><img id="Codehighlighter1_369_446_Open_Image" onclick="this.style.display='none'; Codehighlighter1_369_446_Open_Text.style.display='none'; Codehighlighter1_369_446_Closed_Image.style.display='inline'; Codehighlighter1_369_446_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_369_446_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_369_446_Closed_Text.style.display='none'; Codehighlighter1_369_446_Open_Image.style.display='inline'; Codehighlighter1_369_446_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">A()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_369_446_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_369_446_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;26</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;27</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;A</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;28</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;29</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;30</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;31</span><span style="color: #000000"><img id="Codehighlighter1_460_612_Open_Image" onclick="this.style.display='none'; Codehighlighter1_460_612_Open_Text.style.display='none'; Codehighlighter1_460_612_Closed_Image.style.display='inline'; Codehighlighter1_460_612_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_460_612_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_460_612_Closed_Text.style.display='none'; Codehighlighter1_460_612_Open_Image.style.display='inline'; Codehighlighter1_460_612_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Do()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_460_612_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_460_612_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;32</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat1;<br /></span><span style="color: #008080">&nbsp;33</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat2;<br /></span><span style="color: #008080">&nbsp;34</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Called&nbsp;Do()&nbsp;by&nbsp;object&nbsp;of&nbsp;A.&nbsp;[</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;35</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat1&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">,&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;36</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat2&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">]</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;37</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;38</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;39</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;40</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;41</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat1;<br /></span><span style="color: #008080">&nbsp;42</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat2;<br /></span><span style="color: #008080">&nbsp;43</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;44</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;45</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;B<br /></span><span style="color: #008080">&nbsp;46</span><span style="color: #000000"><img id="Codehighlighter1_666_896_Open_Image" onclick="this.style.display='none'; Codehighlighter1_666_896_Open_Text.style.display='none'; Codehighlighter1_666_896_Closed_Image.style.display='inline'; Codehighlighter1_666_896_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_666_896_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_666_896_Closed_Text.style.display='none'; Codehighlighter1_666_896_Open_Image.style.display='inline'; Codehighlighter1_666_896_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_666_896_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_666_896_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;47</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;48</span><span style="color: #000000"><img id="Codehighlighter1_701_783_Open_Image" onclick="this.style.display='none'; Codehighlighter1_701_783_Open_Text.style.display='none'; Codehighlighter1_701_783_Closed_Image.style.display='inline'; Codehighlighter1_701_783_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_701_783_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_701_783_Closed_Text.style.display='none'; Codehighlighter1_701_783_Open_Image.style.display='inline'; Codehighlighter1_701_783_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;B(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;nID)&nbsp;:&nbsp;m_nID(nID)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_701_783_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_701_783_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;49</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;B:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nID&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;50</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">&nbsp;51</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;52</span><span style="color: #000000"><img id="Codehighlighter1_791_872_Open_Image" onclick="this.style.display='none'; Codehighlighter1_791_872_Open_Text.style.display='none'; Codehighlighter1_791_872_Closed_Image.style.display='inline'; Codehighlighter1_791_872_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_791_872_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_791_872_Closed_Text.style.display='none'; Codehighlighter1_791_872_Open_Image.style.display='inline'; Codehighlighter1_791_872_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">B()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_791_872_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_791_872_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;53</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;B:&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nID&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;54</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">&nbsp;55</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;56</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;57</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;58</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nID;<br /></span><span style="color: #008080">&nbsp;59</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;60</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;61</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">class</span><span style="color: #000000">&nbsp;C<br /></span><span style="color: #008080">&nbsp;62</span><span style="color: #000000"><img id="Codehighlighter1_908_1327_Open_Image" onclick="this.style.display='none'; Codehighlighter1_908_1327_Open_Text.style.display='none'; Codehighlighter1_908_1327_Closed_Image.style.display='inline'; Codehighlighter1_908_1327_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_908_1327_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_908_1327_Closed_Text.style.display='none'; Codehighlighter1_908_1327_Open_Image.style.display='inline'; Codehighlighter1_908_1327_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_908_1327_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_908_1327_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;63</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;64</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;C&nbsp;ms_instance;<br /></span><span style="color: #008080">&nbsp;65</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;66</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;67</span><span style="color: #000000"><img id="Codehighlighter1_976_1001_Open_Image" onclick="this.style.display='none'; Codehighlighter1_976_1001_Open_Text.style.display='none'; Codehighlighter1_976_1001_Closed_Image.style.display='inline'; Codehighlighter1_976_1001_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_976_1001_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_976_1001_Closed_Text.style.display='none'; Codehighlighter1_976_1001_Open_Image.style.display='inline'; Codehighlighter1_976_1001_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;C</span><span style="color: #000000">&amp;</span><span style="color: #000000">&nbsp;GetInstance()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_976_1001_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_976_1001_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;68</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;ms_instance;<br /></span><span style="color: #008080">&nbsp;69</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;70</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;71</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;72</span><span style="color: #000000"><img id="Codehighlighter1_1032_1093_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1032_1093_Open_Text.style.display='none'; Codehighlighter1_1032_1093_Closed_Image.style.display='inline'; Codehighlighter1_1032_1093_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1032_1093_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1032_1093_Closed_Text.style.display='none'; Codehighlighter1_1032_1093_Open_Image.style.display='inline'; Codehighlighter1_1032_1093_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;C()&nbsp;:&nbsp;m_nStat(</span><span style="color: #000000">-</span><span style="color: #000000">1</span><span style="color: #000000">)&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1032_1093_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1032_1093_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;73</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Construct&nbsp;C</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;74</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;75</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;76</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;C(</span><span style="color: #0000ff">const</span><span style="color: #000000">&nbsp;C</span><span style="color: #000000">&amp;</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;77</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;78</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">public</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;79</span><span style="color: #000000"><img id="Codehighlighter1_1124_1184_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1124_1184_Open_Text.style.display='none'; Codehighlighter1_1124_1184_Closed_Image.style.display='inline'; Codehighlighter1_1124_1184_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1124_1184_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1124_1184_Closed_Text.style.display='none'; Codehighlighter1_1124_1184_Open_Image.style.display='inline'; Codehighlighter1_1124_1184_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">~</span><span style="color: #000000">C()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1124_1184_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1124_1184_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;80</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Destruct&nbsp;C</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;81</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nStat&nbsp;</span><span style="color: #000000">=</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;82</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;83</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;84</span><span style="color: #000000"><img id="Codehighlighter1_1198_1301_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1198_1301_Open_Text.style.display='none'; Codehighlighter1_1198_1301_Closed_Image.style.display='inline'; Codehighlighter1_1198_1301_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockStart.gif"><img style="display: none" id="Codehighlighter1_1198_1301_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1198_1301_Closed_Text.style.display='none'; Codehighlighter1_1198_1301_Open_Image.style.display='inline'; Codehighlighter1_1198_1301_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedSubBlock.gif">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">void</span><span style="color: #000000">&nbsp;Do()&nbsp;</span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1198_1301_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1198_1301_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">&nbsp;85</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">++</span><span style="color: #000000">m_nStat;<br /></span><span style="color: #008080">&nbsp;86</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Called&nbsp;Do()&nbsp;by&nbsp;object&nbsp;of&nbsp;C.&nbsp;[</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;87</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;m_nStat&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">]</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;<br /></span><span style="color: #008080">&nbsp;88</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">&nbsp;89</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" />&nbsp;&nbsp;&nbsp;&nbsp;}</span></span><span style="color: #000000"><br /></span><span style="color: #008080">&nbsp;90</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">&nbsp;91</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /></span><span style="color: #0000ff">private</span><span style="color: #000000">:<br /></span><span style="color: #008080">&nbsp;92</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;m_nStat;<br /></span><span style="color: #008080">&nbsp;93</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span><span style="color: #000000">;<br /></span><span style="color: #008080">&nbsp;94</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">&nbsp;95</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;B&nbsp;gs_B0(</span><span style="color: #000000">0</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;96</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />B&nbsp;g_B1(</span><span style="color: #000000">1</span><span style="color: #000000">);<br /></span><span style="color: #008080">&nbsp;97</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />A&nbsp;A::ms_instance;<br /></span><span style="color: #008080">&nbsp;98</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />C&nbsp;C::ms_instance;<br /></span><span style="color: #008080">&nbsp;99</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">static</span><span style="color: #000000">&nbsp;B&nbsp;gs_B2(</span><span style="color: #000000">2</span><span style="color: #000000">);<br /></span><span style="color: #008080">100</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" />B&nbsp;g_B3(</span><span style="color: #000000">3</span><span style="color: #000000">);<br /></span><span style="color: #008080">101</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /><br /></span><span style="color: #008080">102</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/None.gif" /></span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;main(</span><span style="color: #0000ff">int</span><span style="color: #000000">&nbsp;argc,&nbsp;</span><span style="color: #0000ff">char</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">*</span><span style="color: #000000">&nbsp;argv[])<br /></span><span style="color: #008080">103</span><span style="color: #000000"><img id="Codehighlighter1_1462_1583_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1462_1583_Open_Text.style.display='none'; Codehighlighter1_1462_1583_Closed_Image.style.display='inline'; Codehighlighter1_1462_1583_Closed_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif"><img style="display: none" id="Codehighlighter1_1462_1583_Closed_Image" onclick="this.style.display='none'; Codehighlighter1_1462_1583_Closed_Text.style.display='none'; Codehighlighter1_1462_1583_Open_Image.style.display='inline'; Codehighlighter1_1462_1583_Open_Text.style.display='inline';" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif"></span><span style="border-bottom: #808080 1px solid; border-left: #808080 1px solid; background-color: #ffffff; display: none; border-top: #808080 1px solid; border-right: #808080 1px solid" id="Codehighlighter1_1462_1583_Closed_Text"><img alt="" src="http://www.cppblog.com/Images/dot.gif" /></span><span id="Codehighlighter1_1462_1583_Open_Text"><span style="color: #000000">{<br /></span><span style="color: #008080">104</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;std::cout&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">"</span><span style="color: #000000">Enter&nbsp;main</span><span style="color: #000000">"</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">&lt;&lt;</span><span style="color: #000000">&nbsp;std::endl;<br /></span><span style="color: #008080">105</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;A::GetInstance().Do();<br /></span><span style="color: #008080">106</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;C::GetInstance().Do();<br /></span><span style="color: #008080">107</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" /><br /></span><span style="color: #008080">108</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;system(</span><span style="color: #000000">"</span><span style="color: #000000">pause</span><span style="color: #000000">"</span><span style="color: #000000">);<br /></span><span style="color: #008080">109</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000">&nbsp;</span><span style="color: #000000">0</span><span style="color: #000000">;<br /></span><span style="color: #008080">110</span><span style="color: #000000"><img alt="" align="top" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" />}</span></span></div>
<p>为了能够分析得细致些，Demo写得长了点，见谅咯！~</p>
<p>&nbsp;&nbsp;&nbsp; 嗯，对了，还有运行结果：</p>
<p><img border="0" alt="" src="http://www.cppblog.com/images/cppblog_com/qingbizhu/Ret_0.PNG" width="351" height="405" /></p>
<p>仔细看看结果，有没觉得这结果很出乎意料啊？！！（顺便提下，我用的编译工具是Visual C++ 2010）</p>
<p>从这个运行结果，对通过静态成员实现的惰性单件，我得到了以下两点：</p>
<ol><li>对于定义在单件对象之前的全局或静态全局对象，虽然单件对象还是会在调用前及时构造，但构造函数内的初始化操作却可能在需要使用时还未能执行完全。<br />如：std::cout 就会使得单件对象的构造函数调用被暂停（单件的其他操作还能继续调用），直到单件之前的全局或静态全局的构造函数全执行完了，才能继续执行。【<span style="color: red">*这个原因还请有知道的高手能赐教。</span>】</li><li>对于定义于单件对象之后的全局或静态全局对象，如果在析构中调用了单件，就会使得单件在释放后又被再次重新创建使用。当然，这时单件内所存的数据已跟之前毫无关联了。</li></ol>
<p>因此，我要奉劝各位开发者，如果在你的全局或静态全局对象的构造或析构方法中调用某个单件，那么对该单件的实现就不要以静态成员来惰性地实现。</p>
<p>&nbsp;</p>   <img src ="http://www.cppblog.com/qingbizhu/aggbug/167681.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/qingbizhu/" target="_blank">青碧竹</a> 2012-03-12 02:04 <a href="http://www.cppblog.com/qingbizhu/archive/2012/03/12/167681.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>