• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            #include<iostream>
            using std::cout;
            using std::endl;
            class CDust{
            public:
                CDust()
                {
                    cout 
            << " CDust constructor " << endl;
                }

                
            ~CDust()
                {

                    cout 
            << " ~CFoo destructor " << endl;
                }
            };


            CDust A;

            int main()
            {
                
            return 0;
            }
            我想類(lèi)似的代碼在網(wǎng)上Google下應(yīng)該有不少
            嘗試了下,在vc6.0的情況下,是沒(méi)有輸出 "~CFoo destructor", 但這并不代表 ~CDust() 沒(méi)有執(zhí)行.
            在~CDust里面設(shè)置斷點(diǎn),會(huì)發(fā)現(xiàn)事實(shí)上程序運(yùn)行時(shí)進(jìn)入了析構(gòu)函數(shù)里  // -_! 表達(dá)好牽強(qiáng)
            本來(lái)想實(shí)在跟蹤這里開(kāi)始程序發(fā)生了什么調(diào)用了什么,發(fā)現(xiàn)功底不足,完全不明白,就先打斷了
            而再在' return 0 ' 語(yǔ)句前面加上斷點(diǎn),會(huì)看到這個(gè)新加的斷點(diǎn)比析構(gòu)里面的斷點(diǎn)先到達(dá),... 
            以現(xiàn)在c++的造詣和vc6.0的了解情況來(lái)看,頭痛了
            為什么 return 0 后程序不是正常結(jié)束了才去執(zhí)行 全局對(duì)象的析構(gòu)?

            改寫(xiě)下代碼
            #include<iostream.h>

            class CDust{
            public:
                CDust()
                {
                    cout 
            << " CDust constructor " << endl;
                }

                
            ~CDust()
                {

                    cout 
            << " ~CFoo destructor " << endl;
                }
            };


            CDust A;

            int main()
            {

                
            return 0;

            }

            這樣用舊版本的頭文件實(shí)現(xiàn),控制臺(tái)輸出了意外的兩個(gè)調(diào)用 --- 析構(gòu)函數(shù)輸出顯示了..

            是cout在頭文件的實(shí)現(xiàn)方式不同?

            在dev-c++里面,運(yùn)行第一代代碼
            根據(jù)斷點(diǎn)的設(shè)置測(cè)試,也是先執(zhí)行了 main()函數(shù)里面的 return 0 才進(jìn)入全局函數(shù)的析構(gòu),也能發(fā)現(xiàn)析構(gòu)函數(shù)里面的輸出被調(diào)用了,控制臺(tái)有明確的顯示
            這樣一來(lái),又不明白了...
            在vc6.0里面為什么執(zhí)行了全局函數(shù)的析構(gòu)卻沒(méi)有所謂的輸出?
            是因?yàn)閏md控制臺(tái)在'return 0'程序權(quán)限收回  // 好像扯到系統(tǒng)的一些混亂的舊記憶了...

            網(wǎng)游了一下
            找到暫時(shí)比較清晰的說(shuō)法是:
               In C++, the constructor of a global object is executed before the main() function(of course after the STARTUP code), while the destructor
            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
            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.).
            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. 
               //感謝提出此說(shuō)法的朋友
             
              _startup才是用戶(hù)程序的起點(diǎn)和終點(diǎn)? 的確,調(diào)用_exit()函數(shù)再斷點(diǎn)測(cè)試,全局對(duì)象的destructor是沒(méi)有進(jìn)入的機(jī)會(huì)   //長(zhǎng)見(jiàn)識(shí)了

              就此先打斷... 再深入今晚就這樣完了.
              就此記錄下,以后再接觸



            不小心又接觸了...
            懶得開(kāi)新的就集中在這里吧

            在main()里面手動(dòng)調(diào)用全局對(duì)象的析構(gòu), 最后程序都會(huì)執(zhí)行兩次析構(gòu)調(diào)用... 在<iostream.h>的cout這種情況下明顯,在std::cout下還得靠斷點(diǎn)設(shè)置才能體現(xiàn)到(vc6的情況)
                 這里是一種解析
                 {
                  Once a destructor is invoked for an object, the object no longer exists; the behavior is undefined if the destructor is invoked for an object whose lifetime has ended. [Example: if the 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
            }
                 非global static object 也會(huì)出現(xiàn)兩次調(diào)用, 區(qū)別只在與一個(gè)在main() 退出之前,一個(gè)在之后...
            (的確,手工調(diào)用析構(gòu)函數(shù)的情況很少出現(xiàn)  -_! )
                 如果我在析構(gòu)里面存在 釋放內(nèi)存 這一類(lèi)實(shí)現(xiàn), 那第二次再次釋放不是容易出問(wèn)題!!!
                 以后遇到這種情況得注意檢測(cè)代碼的添加...

            Feedback

            # re: 關(guān)于c++對(duì)象全局對(duì)象析構(gòu)的幾點(diǎn)記錄  回復(fù)  更多評(píng)論   

            2009-02-26 12:05 by 陳梓瀚(vczh)
            說(shuō)不定因?yàn)閏out被析構(gòu)了,你的代碼就沒(méi)看到輸出了。

            # re: 關(guān)于c++對(duì)象全局對(duì)象析構(gòu)的幾點(diǎn)記錄  回復(fù)  更多評(píng)論   

            2009-02-27 19:28 by 藍(lán)塵
            @陳梓瀚(vczh)
            std::cout 被析構(gòu), 而且是只在vc6.0下面的 global static object ? 也就是說(shuō)剩下的就是編譯器的實(shí)現(xiàn)問(wèn)題了(網(wǎng)上的說(shuō)法好像只有std::cout在vc6有這個(gè)情況)
            等有時(shí)間時(shí)調(diào)試跟蹤下...
            多謝提醒了

            Copyright © 藍(lán)塵

            亚洲乱码中文字幕久久孕妇黑人| 91精品国产综合久久婷婷| 久久99精品国产99久久6| 亚洲精品国产综合久久一线| 精品国产日韩久久亚洲| 精品久久一区二区三区| 久久久久97国产精华液好用吗| 2021国内精品久久久久久影院| 91精品国产高清91久久久久久| 久久无码国产| 久久久久久久尹人综合网亚洲| 国产亚洲精久久久久久无码AV| 色综合久久无码中文字幕| 亚洲美日韩Av中文字幕无码久久久妻妇| 国产精品久久久99| 久久国产热精品波多野结衣AV| 亚洲国产精品嫩草影院久久| 国产成人精品综合久久久久 | 久久久久久精品久久久久| AV色综合久久天堂AV色综合在| 久久久久亚洲AV成人网人人网站| 色婷婷综合久久久中文字幕| 香蕉久久夜色精品国产尤物 | 亚洲国产成人久久综合区| 欧美日韩中文字幕久久伊人| 看久久久久久a级毛片| 欧美精品乱码99久久蜜桃| 久久久精品波多野结衣| 国产高潮国产高潮久久久91| 99国产精品久久| 日韩人妻无码精品久久免费一| 尹人香蕉久久99天天拍| 欧美午夜A∨大片久久| 欧美国产成人久久精品| 国产精品成人99久久久久91gav| 伊人热人久久中文字幕| 色综合久久88色综合天天| 99久久夜色精品国产网站| 久久精品国产欧美日韩| 久久免费99精品国产自在现线| 久久精品国产亚洲Aⅴ香蕉|