• <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>
            隨筆 - 87  文章 - 279  trackbacks - 0
            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            潛心看書(shū)研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊(cè)

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 217755
            • 排名 - 117

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            智能指針源碼,可像普通指針一樣使用,但可以自動(dòng)釋放內(nèi)存,代碼很短,看一下就明白原理了。
            #ifndef AUTOPTR
            #define AUTOPTR

            /**
             * 智能指針類
             
            */

            template
            <class T>
            class AutoPtr {
            public :
                AutoPtr(T
            * p = 0) : pointee(p) {} //默認(rèn)構(gòu)造函數(shù)

                template
            <class U>
                    AutoPtr(AutoPtr
            <U>& rhs) : pointee(rhs.release()) {}//復(fù)制構(gòu)造函數(shù)

                
            ~AutoPtr() {delete pointee;}

                template
            <class U>
                AutoPtr
            <T>& operator=(AutoPtr<U>& rhs) //賦值函數(shù)
                    if (this != &rhs) {
                        reset(rhs.release());
                    }

                    
            return *this;
                }


                T
            & operator*() const {return *pointee;} 
                
                T
            * operator->() const {return pointee;}

                T
            * get() const {return pointee;} //獲取dumb pointer

                T
            * release() //釋放dumb pointer 的擁有權(quán),并返回其值
                    T* oldPointee == pointee;
                    pointee 
            = 0;
                    
            return oldPointee;
                }
             
                
                
            void reset(T* p=0//重復(fù)置p指針
                    if (pointee != p) {
                        delete pointee;
                        pointee 
            = p;
                    }

                }


            private :
                T
            * pointee;
            }
            ;

            #endif AUTOPTR

            test.cpp
            #include "AutoPtr.h"
            #include 
            <iostream>
            #include 
            <string>
            using namespace std;

            int main() {
                AutoPtr
            <int> p = new int;
                
            *= 100;
                printf(
            "%d\n"*p);

                AutoPtr
            <string> sp = new string;
                
            *sp = "hello world";
                printf(
            "%s\n", sp->c_str());
                
            return 0;
            }

            posted on 2008-04-17 17:01 閱讀(1007) 評(píng)論(3)  編輯 收藏 引用 所屬分類: C++之夢(mèng)

            FeedBack:
            # re: 智能指針源碼 2008-07-11 14:16 fr3@K
            你的實(shí)現(xiàn)沒(méi)辦法像這樣使用:

            AutoPtr<int> foo()
            {
            return AutoPtr<int>(new int);
            }

            void bar()
            {
            AutoPtr<int> p = foo();
            }
              回復(fù)  更多評(píng)論
              
            # re: 智能指針源碼 2008-07-15 19:40 hah
            @fr3@K
            本來(lái)就不應(yīng)該返回指向局部變量的指針  回復(fù)  更多評(píng)論
              
            # re: 智能指針源碼 2008-07-16 01:45 fr3@K
            std::auto_ptr 就可以. 請(qǐng)參考這篇 (http://www.gotw.ca/publications/using_auto_ptr_effectively.htm) 的 example 7 的用法說(shuō)明.

            實(shí)作上可以參考這里 (http://code.google.com/p/gion/source/browse/branches/0.1/include/gion/auto_array.hpp), 找與 auto_array_ref 相關(guān)的代碼.  回復(fù)  更多評(píng)論
              
            久久综合久久综合久久综合| 久久中文字幕视频、最近更新| 欧美亚洲国产精品久久| 久久精品无码一区二区WWW| 狠狠色婷婷久久综合频道日韩| 午夜不卡久久精品无码免费| 久久青青草原精品影院| 色狠狠久久综合网| 久久97精品久久久久久久不卡| 国产午夜精品理论片久久| 久久经典免费视频| 精品久久久久久| 亚洲乱码中文字幕久久孕妇黑人| 97久久精品午夜一区二区| 久久亚洲精品无码播放| 91久久精品91久久性色| 伊人久久国产免费观看视频| 国产精品久久久久天天影视| 久久青青色综合| 国产一区二区三区久久| 久久久久亚洲国产| 久久久久一级精品亚洲国产成人综合AV区| 久久久久久精品成人免费图片| 精品国产91久久久久久久a| 久久精品夜夜夜夜夜久久| 伊人热热久久原色播放www | 国产成人久久精品二区三区| 伊人久久五月天| 久久强奷乱码老熟女网站| 国产成人无码精品久久久久免费| 狼狼综合久久久久综合网| 2021最新久久久视精品爱| 久久亚洲精品无码观看不卡| 久久精品国产99久久香蕉| 久久久精品免费国产四虎| 99久久婷婷免费国产综合精品| 狠狠色丁香久久婷婷综合_中 | 久久久久99精品成人片试看 | 久久亚洲精品国产精品| 亚洲国产视频久久| 久久国产精品成人免费|