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

            改寫下代碼
            #include<iostream.h>

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

                
            ~CDust()
                {

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


            CDust A;

            int main()
            {

                
            return 0;

            }

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

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

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

            網(wǎng)游了一下
            找到暫時比較清晰的說法是:
               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. 
               //感謝提出此說法的朋友
             
              _startup才是用戶程序的起點和終點? 的確,調(diào)用_exit()函數(shù)再斷點測試,全局對象的destructor是沒有進(jìn)入的機(jī)會   //長見識了

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



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

            在main()里面手動調(diào)用全局對象的析構(gòu), 最后程序都會執(zhí)行兩次析構(gòu)調(diào)用... 在<iostream.h>的cout這種情況下明顯,在std::cout下還得靠斷點設(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 也會出現(xiàn)兩次調(diào)用, 區(qū)別只在與一個在main() 退出之前,一個在之后...
            (的確,手工調(diào)用析構(gòu)函數(shù)的情況很少出現(xiàn)  -_! )
                 如果我在析構(gòu)里面存在 釋放內(nèi)存 這一類實現(xiàn), 那第二次再次釋放不是容易出問題!!!
                 以后遇到這種情況得注意檢測代碼的添加...

            Feedback

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

            2009-02-26 12:05 by 陳梓瀚(vczh)
            說不定因為cout被析構(gòu)了,你的代碼就沒看到輸出了。

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

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

            Copyright © 藍(lán)塵

            亚洲精品高清一二区久久| 久久婷婷五月综合色高清| 婷婷综合久久中文字幕蜜桃三电影 | 国产精品美女久久久免费| 久久亚洲综合色一区二区三区 | 久久露脸国产精品| 婷婷久久香蕉五月综合加勒比| 午夜精品久久久久久99热| 久久国产精品久久| 日韩精品久久久肉伦网站| 99久久精品国内| 国产美女久久久| 免费精品久久天干天干| 久久最新免费视频| 久久九九精品99国产精品| 久久精品青青草原伊人| 久久国产精品波多野结衣AV| 精品午夜久久福利大片| 久久精品国产男包| 狠狠色婷婷久久综合频道日韩| 亚洲国产成人乱码精品女人久久久不卡 | 欧美日韩精品久久久免费观看| 久久久久国产一级毛片高清版| 久久天天躁狠狠躁夜夜2020老熟妇| 中文字幕乱码人妻无码久久 | 久久久国产精华液| 久久久久久A亚洲欧洲AV冫| 久久精品亚洲乱码伦伦中文 | 久久伊人五月天论坛| 九九久久99综合一区二区| 色欲久久久天天天综合网精品 | 国产精品国色综合久久| 99精品久久精品| 久久人人爽人人爽人人片AV不 | 久久er99热精品一区二区| 久久精品一区二区三区AV| 久久受www免费人成_看片中文| 久久久久国产成人精品亚洲午夜| 久久国产亚洲精品麻豆| 久久国产乱子精品免费女| 国产ww久久久久久久久久|