• <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運(yùn)用實(shí)例:常數(shù)型智能指針的特性

            //展示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進(jìn)行提領(lǐng)(dereference)操作,C++標(biāo)準(zhǔn)規(guī)格,
                
            //這會導(dǎo)致未定義行為,比較如說導(dǎo)致程序的崩潰。
                *= -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
            }

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

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

            導(dǎo)航

            <2025年8月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            統(tǒng)計(jì)

            留言簿(6)

            隨筆分類(109)

            隨筆檔案(105)

            Blogers

            Game

            Life

            NodeJs

            Python

            Useful Webs

            大牛

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久最新免费视频| 久久se精品一区精品二区国产 | 91精品婷婷国产综合久久| 久久久久久国产精品美女| 久久精品青青草原伊人| 久久精品www| 亚洲va中文字幕无码久久不卡| 国产成人精品久久一区二区三区| 欧美久久久久久精选9999| 国产成年无码久久久久毛片| 超级碰碰碰碰97久久久久| 久久综合综合久久狠狠狠97色88| 精品国产乱码久久久久软件| 国产毛片久久久久久国产毛片| 久久国产欧美日韩精品| 国产69精品久久久久观看软件| 精品久久久无码中文字幕| 97久久超碰国产精品2021| 无码伊人66久久大杳蕉网站谷歌| 欧美久久一级内射wwwwww.| 久久久国产精品福利免费| 久久久一本精品99久久精品66| 国内精品久久久久影院亚洲| 久久久中文字幕日本| 办公室久久精品| 好久久免费视频高清| 99久久国产综合精品麻豆| 天堂久久天堂AV色综合| 2020国产成人久久精品| 久久久久亚洲精品无码网址 | 天天做夜夜做久久做狠狠| 久久综合九色综合欧美就去吻| 九九久久精品国产| 久久伊人五月天论坛| 欧美粉嫩小泬久久久久久久 | 欧美777精品久久久久网| 久久久综合九色合综国产| 久久不射电影网| 久久久黄片| 国内精品伊人久久久久777| 久久精品人人做人人爽电影|