• <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
            <2008年4月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            潛心看書研究!

            常用鏈接

            留言簿(19)

            隨筆分類(81)

            文章分類(89)

            相冊

            ACM OJ

            My friends

            搜索

            •  

            積分與排名

            • 積分 - 217747
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

            智能指針源碼,可像普通指針一樣使用,但可以自動釋放內(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) 評論(3)  編輯 收藏 引用 所屬分類: C++之夢

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

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

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

            實作上可以參考這里 (http://code.google.com/p/gion/source/browse/branches/0.1/include/gion/auto_array.hpp), 找與 auto_array_ref 相關(guān)的代碼.  回復(fù)  更多評論
              
            久久涩综合| 三级三级久久三级久久| 色综合久久久久网| 青青青青久久精品国产 | 精品久久久久久久中文字幕| 久久久久久A亚洲欧洲AV冫| 人人妻久久人人澡人人爽人人精品| 国产精品久久久久久久app| 亚洲AV乱码久久精品蜜桃| 国产精品视频久久久| 亚洲精品97久久中文字幕无码| 日韩乱码人妻无码中文字幕久久 | 久久精品国产99国产精品导航| 国产精品对白刺激久久久| 久久久久亚洲AV成人网人人软件| 久久精品国产乱子伦| 久久强奷乱码老熟女| 色综合合久久天天综合绕视看| 久久久久久精品久久久久| 国产综合精品久久亚洲| 久久精品国产亚洲av高清漫画| 精品久久久久久久久免费影院| 久久天堂电影网| 2022年国产精品久久久久| 狠狠综合久久AV一区二区三区| 久久露脸国产精品| 国产精品成人久久久久三级午夜电影| 久久亚洲熟女cc98cm| 亚洲?V乱码久久精品蜜桃| 久久精品这里只有精99品| 久久精品视频网| 国产精品一区二区久久国产| 久久人人爽人人爽人人片AV麻烦| 日日狠狠久久偷偷色综合96蜜桃| 亚洲午夜久久影院| 久久99精品国产99久久| 欧美精品一区二区精品久久| 青青青国产成人久久111网站| 久久久久中文字幕| 激情综合色综合久久综合| 久久精品国产一区二区三区日韩|