• <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>

            luqingfei@C++

            為中華之崛起而崛起!
            兼聽則明,偏聽則暗。

            智能指針auto_ptr運用實例:常數型智能指針的特性

            //展示const auto_ptr的特性
            #include <iostream>
            #include 
            <memory>
            using namespace std;

            /* define output operator for auto_ptr
            *  print object value or NULL
            */
            template 
            <class T>
            ostream
            & operator<< (ostream& strm, const auto_ptr<T>& p)
            {
                
            // does p own an object ? 
                if (p.get() == NULL)
                {
                    strm 
            << "NULL"// NO: print NULL
                }
                
            else
                {
                    strm 
            << *p;     // YES: print the object
                }

                
            return strm;
            }

            int main()
            {
                
            const auto_ptr<int> p(new int(42));
                
            const auto_ptr<int> q(new int (0));
                
            const auto_ptr<int> r;

                cout 
            << "after initialization:" << endl;
                cout 
            << " p: " << p << endl;
                cout 
            << " q: " << q << endl;
                cout 
            << " r: " << r << endl;

                
            *= *p;
                
            //*r = *p;    // ERROR: undefined behavior 對于一個“未指向任何對象”的auto_ptr進行提領(dereference)操作,C++標準規格,
                
            //這會導致未定義行為,比較如說導致程序的崩潰。
                *= -77;

                cout 
            << "after assigning values:" << endl;
                cout 
            << " p: " << p << endl;
                cout 
            << " q: " << q << endl;
                cout 
            << " r: " << r << endl;

                
            //q = p; // ERROR at compile time
                
            //r = p; // ERROR at compile time
            }

            輸出結果:
            after initialization:
             p: 42
             q: 0
             r: NULL
            after assigning values:
             p: -77
             q: 42
             r: NULL
            請按任意鍵繼續. . .

            posted on 2011-01-04 15:29 luqingfei 閱讀(662) 評論(0)  編輯 收藏 引用 所屬分類: C++基礎

            導航

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            統計

            留言簿(6)

            隨筆分類(109)

            隨筆檔案(105)

            Blogers

            Game

            Life

            NodeJs

            Python

            Useful Webs

            大牛

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲AV日韩精品久久久久| 久久久久人妻精品一区| 亚洲欧美日韩精品久久| 99久久亚洲综合精品网站| 蜜桃麻豆www久久国产精品| 久久婷婷国产剧情内射白浆| 久久水蜜桃亚洲av无码精品麻豆| 久久亚洲精品视频| 伊人久久大香线蕉综合热线| 久久天堂AV综合合色蜜桃网 | 国产成人久久激情91| 美女写真久久影院| 久久久久久久97| 91亚洲国产成人久久精品网址| 免费无码国产欧美久久18| 久久99精品国产99久久| 偷偷做久久久久网站| 国产2021久久精品| 99久久人妻无码精品系列蜜桃| 久久亚洲欧洲国产综合| 久久天天躁狠狠躁夜夜网站 | 国产免费久久精品丫丫| 综合网日日天干夜夜久久| 国产69精品久久久久9999APGF | 日本欧美久久久久免费播放网| 曰曰摸天天摸人人看久久久| 久久精品国产亚洲av水果派| 综合久久一区二区三区 | 亚洲一区中文字幕久久| 国内精品久久久人妻中文字幕 | 久久精品免费观看| 国产精品久久久久jk制服| 99久久国产精品免费一区二区| 久久se精品一区二区影院| 99久久国产热无码精品免费久久久久| 日日噜噜夜夜狠狠久久丁香五月| 久久99精品久久久大学生| 性欧美大战久久久久久久久| 亚洲成色WWW久久网站| 久久综合九色综合网站| 国产精品美女久久久久|