﻿<?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/lazz/category/9852.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 11 Jun 2009 08:45:31 GMT</lastBuildDate><pubDate>Thu, 11 Jun 2009 08:45:31 GMT</pubDate><ttl>60</ttl><item><title>关于无临时变量的变量值交换的记录 &amp;&amp; 求最大表示数</title><link>http://www.cppblog.com/lazz/archive/2009/06/11/87410.html</link><dc:creator>蓝尘</dc:creator><author>蓝尘</author><pubDate>Thu, 11 Jun 2009 07:15:00 GMT</pubDate><guid>http://www.cppblog.com/lazz/archive/2009/06/11/87410.html</guid><wfw:comment>http://www.cppblog.com/lazz/comments/87410.html</wfw:comment><comments>http://www.cppblog.com/lazz/archive/2009/06/11/87410.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lazz/comments/commentRss/87410.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lazz/services/trackbacks/87410.html</trackback:ping><description><![CDATA[源于无聊, 又再看了复习了下"<span class="Apple-style-span" style="border-collapse: separate; color: #000000; font-family: 'Times New Roman'; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;">不用临时变量进行整型变量</span>交换"<br><br>两种流行方法:<br>(1) 加减减<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;AsExchang(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">y)<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">}<br></span><span style="color: #008080;">7</span>&nbsp;<span style="color: #000000;"></span></div>
<br>(2) 异或<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">1</span>&nbsp;<span style="color: #0000ff;">void</span><span style="color: #000000;">&nbsp;XorExchang(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">x,&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">&amp;</span><span style="color: #000000;">y)<br></span><span style="color: #008080;">2</span>&nbsp;<span style="color: #000000;">{<br></span><span style="color: #008080;">3</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">4</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">5</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;x&nbsp;</span><span style="color: #000000;">^</span><span style="color: #000000;">&nbsp;y;<br></span><span style="color: #008080;">6</span>&nbsp;<span style="color: #000000;">}</span></div>
<br>两种方法, 都可以认识是密码学的一部分吧, 只是用加减法来加密, 幼稚且缺乏安全性而已.<br>当然, 异或法在现实中也得变种才能实际应用<br><br>一开始傻傻地联想到方法一是否存在越界问题... //基础知识瞬间薄弱化...<br>系统底层的实现居然一时间忘记了...<br><br>联想到如何确认一个类型能表示的最大最小值<br>(1)<br>所以想到的用sizeof运算符, 判断类型的位数, 然后笨笨地左移右移操作进行计算<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;getMaxInt()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;iBase</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">const</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;IntSize&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)&nbsp;</span><span style="color: #000000;">*</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">8</span><span style="color: #000000;">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">int是有符号数,&nbsp;最左边一位是符号位<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #008000;">//</span><span style="color: #008000;">so</span><span style="color: #008000;"><br></span><span style="color: #000000;">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">for</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;i&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;&nbsp;i&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">&nbsp;IntSize&nbsp;</span><span style="color: #000000;">-</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">&nbsp;;&nbsp;i</span><span style="color: #000000;">++</span><span style="color: #000000;">&nbsp;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iBase&nbsp;</span><span style="color: #000000;">=</span><span style="color: #000000;">&nbsp;(iBase&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;i)&nbsp;</span><span style="color: #000000;">+</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">1</span><span style="color: #000000;">;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000ff;">return</span><span style="color: #000000;">&nbsp;iBase;&nbsp;<br>}</span></div>
好笨的方法...<br>很没效率<br><br>(2)<br>用强制转换吧, (int)0x*,<br>*是什么, 0x7fffffff <br>恶心-_-||, 我一开始又4个f了, 一个f才4位吧...<br><br>(3)<br>还有用bitset, bitsit有to_ulong方法, 凑合着用<br><br>(4)<br>climits的 INT_MAX<br>在cplusplus.com上面的reference是显示<br>
<table class="boxed">
    <tbody>
        <tr>
            <td><tt>INT_MAX</tt></td>
            <td>&nbsp;Maximum value for an object of type
            <tt>int <br></tt></td>
            <td><tt>&nbsp;= 32767</tt></td>
        </tr>
    </tbody>
</table>
被他的图表骗了<br><br>VC6版本的头文件里面是用<br>#define INT_MAX&nbsp; 2147483647&nbsp;
<br>直接定义的<br>查看cywin里面的头文件<br>也差不多..<br>#define __INT_MAX__ 2147483647
<br>#define INT_MAX __INT_MAX__
<br><br><br>(5)<br>C++标准方法..<br>//直接贴过来了<br>//前面有说明说numeric_limits&lt;int&gt;::max()等于INT_MAX<br>//... 只是封装了模板而已<br>
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #008080;">&nbsp;1</span>&nbsp;<span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;numeric_limits&nbsp;example</span><span style="color: #008000;"><br></span><span style="color: #008080;">&nbsp;2</span>&nbsp;<span style="color: #008000;"></span><span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;3</span>&nbsp;<span style="color: #000000;">#include&nbsp;</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">limits</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;4</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;">&nbsp;std;<br></span><span style="color: #008080;">&nbsp;5</span>&nbsp;<span style="color: #000000;"><br></span><span style="color: #008080;">&nbsp;6</span>&nbsp;<span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main&nbsp;()&nbsp;{<br></span><span style="color: #008080;">&nbsp;7</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;boolalpha;<br></span><span style="color: #008080;">&nbsp;8</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Minimum&nbsp;value&nbsp;for&nbsp;int:&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;numeric_limits</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::min()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">&nbsp;9</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Maximum&nbsp;value&nbsp;for&nbsp;int:&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;numeric_limits</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::max()&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">10</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">int&nbsp;is&nbsp;signed:&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;numeric_limits</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::is_signed&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">11</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">Non-sign&nbsp;bits&nbsp;in&nbsp;int:&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;numeric_limits</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::digits&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">12</span>&nbsp;<span style="color: #000000;">&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">int&nbsp;has&nbsp;infinity:&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;numeric_limits</span><span style="color: #000000;">&lt;</span><span style="color: #0000ff;">int</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">::has_infinity&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;endl;<br></span><span style="color: #008080;">13</span>&nbsp;<span style="color: #000000;">&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;">14</span>&nbsp;<span style="color: #000000;">}</span></div>
<br><br><br>没去找高手们的奇招, 浪浪荡荡又消费了不少时间...<br>继续潜修去<br><br><img src ="http://www.cppblog.com/lazz/aggbug/87410.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lazz/" target="_blank">蓝尘</a> 2009-06-11 15:15 <a href="http://www.cppblog.com/lazz/archive/2009/06/11/87410.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于c++对象全局对象析构的几点记录</title><link>http://www.cppblog.com/lazz/archive/2009/02/25/74904.html</link><dc:creator>蓝尘</dc:creator><author>蓝尘</author><pubDate>Wed, 25 Feb 2009 12:35:00 GMT</pubDate><guid>http://www.cppblog.com/lazz/archive/2009/02/25/74904.html</guid><wfw:comment>http://www.cppblog.com/lazz/comments/74904.html</wfw:comment><comments>http://www.cppblog.com/lazz/archive/2009/02/25/74904.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cppblog.com/lazz/comments/commentRss/74904.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lazz/services/trackbacks/74904.html</trackback:ping><description><![CDATA[<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;std::cout;<br></span><span style="color: #0000ff;">using</span><span style="color: #000000;">&nbsp;std::endl;<br></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;CDust{<br></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br>&nbsp;&nbsp;&nbsp;&nbsp;CDust()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;CDust&nbsp;constructor&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;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">~</span><span style="color: #000000;">CDust()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;~CFoo&nbsp;destructor&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;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>};<br><br><br>CDust A;<br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br>&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></div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">我想类似的代码在网上Google下应该有不少</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">尝试了下,在vc6.0的情况下,是没有输出 "~CFoo&nbsp;destructor", 但这并不代表 ~CDust() 没有执行.</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">在~CDust里面设置断点,会发现事实上程序运行时进入了析构函数里 &nbsp;// -_! 表达好牵强</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">本来想实在跟踪这里开始程序发生了什么调用了什么,发现功底不足,完全不明白,就先打断了</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">而再在' return 0 ' 语句前面加上断点,会看到这个新加的断点比析构里面的断点先到达,...&nbsp;</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">以现在c++的造诣和vc6.0的了解情况来看,头痛了</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">为什么 return 0 后程序不是正常结束了才去执行 全局对象的析构?</div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;"><br></div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">改写下代码<br></div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">
<div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">iostream.h</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"><br><br></span><span style="color: #0000ff;">class</span><span style="color: #000000;">&nbsp;CDust{<br></span><span style="color: #0000ff;">public</span><span style="color: #000000;">:<br>&nbsp;&nbsp;&nbsp;&nbsp;CDust()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;CDust&nbsp;constructor&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;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #000000;">~</span><span style="color: #000000;">CDust()<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</span><span style="color: #000000;">&lt;&lt;</span><span style="color: #000000;">&nbsp;</span><span style="color: #000000;">"</span><span style="color: #000000;">&nbsp;~CFoo&nbsp;destructor&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;endl;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>};<br><br><br>CDust&nbsp;A;<br><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;">&nbsp;main()<br>{<br><br>&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><br>}</span></div>
<br></div>
<div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border-left-color: #cccccc; width: 98%;">这样用旧版本的头文件实现,控制台输出了意外的两个调用 --- 析构函数输出显示了..<br><br>是cout在头文件的实现方式不同?<br><br>在dev-c++里面,运行第一代代码<br>根据断点的设置测试,也是先执行了 main()函数里面的 return 0 才进入全局函数的析构,也能发现析构函数里面的输出被调用了,控制台有明确的显示<br>这样一来,又不明白了...<br>在vc6.0里面为什么执行了全局函数的析构却没有所谓的输出?<br>是因为cmd控制台在'return 0'程序权限收回&nbsp; // 好像扯到系统的一些混乱的旧记忆了...<br><br>网游了一下<br>找到暂时比较清晰的说法是:<br>&nbsp;&nbsp; In C++, the constructor of a global object is executed before the
main() function(of course after the STARTUP code), while the destructor
<br>
is invoked after the main() function. So in my humble opinion, the
main() function is a bridge between the constructor and the
destructor.Further more, the constructor and the destructor is the
actual manager of the whole program, because they can manage all the<br>
resources of the program(for instance, the constructor allocate memory
units and the destructor free them.I'am not sure of this, any comments
will be appreciated in advance.).<br>
4)In C++, is it true that the resources obtained during the constructor
and the destructor (both belong to a global object)are managed by
themselves and have nothing with the main() function.Therfore, I think
the main() function is not the king in C++ as it is in C.&nbsp; <br>&nbsp;&nbsp; //感谢提出此说法的朋友<br>&nbsp;<br>&nbsp; _startup才是用户程序的起点和终点? 的确,调用_exit()函数再断点测试,全局对象的destructor是没有进入的机会&nbsp;&nbsp; //长见识了<br><br>&nbsp; 就此先打断... 再深入今晚就这样完了.<br>&nbsp; 就此记录下,以后再接触<br>
<br><br><br>不小心又接触了...<br>懒得开新的就集中在这里吧<br><br>在main()里面手动调用全局对象的析构, 最后程序都会执行两次析构调用... 在&lt;iostream.h&gt;的cout这种情况下明显,在std::cout下还得靠断点设置才能体现到(vc6的情况)<br>&nbsp;&nbsp;&nbsp;&nbsp; 这里是一种解析<br>&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Once a <strong class="highlight">destructor  is invoked for an object, the object no longer exists; the behavior is undefined if the <strong class="highlight">destructor  is invoked for an object whose lifetime has ended. [Example: if the <strong class="highlight">destructor
for an automatic object is explicitly invoked, and the block is
subsequently left in a manner that would ordinarily invoke implicit
destruction of the object, the behavior is undefined
</strong></strong></strong><br>}<br>&nbsp;&nbsp;&nbsp;&nbsp; 非global static object 也会出现两次调用, 区别只在与一个在main() 退出之前,一个在之后...<br>(的确,手工调用析构函数的情况很少出现&nbsp; -_! )<br>&nbsp;&nbsp;&nbsp;&nbsp; 如果我在析构里面存在 释放内存 这一类实现, 那第二次再次释放不是容易出问题!!! <br>&nbsp;&nbsp;&nbsp;&nbsp; 以后遇到这种情况得注意检测代码的添加...<br><br></div><img src ="http://www.cppblog.com/lazz/aggbug/74904.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lazz/" target="_blank">蓝尘</a> 2009-02-25 20:35 <a href="http://www.cppblog.com/lazz/archive/2009/02/25/74904.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>