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

            搜索

            •  

            積分與排名

            • 積分 - 217934
            • 排名 - 117

            最新評論

            閱讀排行榜

            評論排行榜

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

            /**
             * 智能指針類
             
            */

            template
            <class T>
            class AutoPtr {
            public :
                AutoPtr(T
            * p = 0) : pointee(p) {} //默認構造函數

                template
            <class U>
                    AutoPtr(AutoPtr
            <U>& rhs) : pointee(rhs.release()) {}//復制構造函數

                
            ~AutoPtr() {delete pointee;}

                template
            <class U>
                AutoPtr
            <T>& operator=(AutoPtr<U>& rhs) //賦值函數
                    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 的擁有權,并返回其值
                    T* oldPointee == pointee;
                    pointee 
            = 0;
                    
            return oldPointee;
                }
             
                
                
            void reset(T* p=0//重復置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
            你的實現沒辦法像這樣使用:

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

            void bar()
            {
            AutoPtr<int> p = foo();
            }
              回復  更多評論
              
            # re: 智能指針源碼 2008-07-15 19:40 hah
            @fr3@K
            本來就不應該返回指向局部變量的指針  回復  更多評論
              
            # 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 相關的代碼.  回復  更多評論
              
            国产精品一区二区久久精品涩爱| 日韩一区二区久久久久久| 久久精品国产精品亚洲人人| 午夜精品久久影院蜜桃| 欧美黑人激情性久久| 久久婷婷色香五月综合激情| 日本一区精品久久久久影院| 成人综合久久精品色婷婷| 久久免费国产精品一区二区| 精品国产乱码久久久久软件| 手机看片久久高清国产日韩| 成人午夜精品久久久久久久小说| 久久综合综合久久综合| 波多野结衣久久一区二区| 国产亚州精品女人久久久久久 | 99久久国产主播综合精品| 久久本道伊人久久| 久久99国产一区二区三区| 国产成人精品久久亚洲高清不卡| 老司机国内精品久久久久| 国产午夜精品理论片久久影视| 精品久久久噜噜噜久久久| 久久黄视频| 久久婷婷五月综合色高清| 国产精品久久久久久搜索| 久久久久久亚洲精品影院| 一本色道久久综合亚洲精品| 欧美日韩中文字幕久久久不卡| 偷窥少妇久久久久久久久| 人妻丰满AV无码久久不卡| 久久国产免费观看精品| 2021国产精品午夜久久| 久久精品免费观看| 国产A级毛片久久久精品毛片| 久久99久久99小草精品免视看 | 99久久免费国产精品特黄| 国内精品伊人久久久久| 国产精品亚洲综合专区片高清久久久| 久久最新免费视频| 少妇精品久久久一区二区三区| 久久人人爽人爽人人爽av|