精英盒子 -> 程序设计 -> [转]自己写的一个智能指针类 [打印本页]

abreto 2012-02-03 12:45

[转]自己写的一个智能指针类

什么是智能指针?传送门:http://baike.baidu.com/view/1391603.htm
然后直接上代码:
  1. #include <stdexcept>

    /**
    * Author: Abreto <m@abreto.net>
    *
    **/
    template <class T>
    class GPointer
    {
    public:
        GPointer(void);
        GPointer(T);
        GPointer(T *);
        GPointer(const GPointer<T> &);
        ~GPointer();
      
        T & operator *(void);
        const T & operator *(void) const;
        T * operator ->(void);
        const T * operator ->(void) const;
      
        T * operator +(unsigned int);
        const T * operator +(unsigned int) const;
    private:
        T*         ptr;
        size_t     use;
      
        void inc_use(void);
        void dec_use(void);
    };

    template <class T>
    void GPointer<T>::inc_use(void)
    {
        use++;
    }

    template <class T>
    void GPointer<T>::dec_use(void)
    {
        if( 0 == (--use) ) delete ptr;
    }

    template <class T>
    GPointer<T>::GPointer(void)
    {
        ptr = new T;
        use = 0;
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(T d)
    {
        ptr = new T(d);
        use = 0;
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(T *p)
    {
        ptr = p;
        use = 0;
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(const GPointer<T> &p)
    {
        ptr = p.ptr;
        use = p.use;
      
        inc_use();
    }

    template <class T>
    GPointer<T>::~GPointer(void)
    {
        dec_use();
    }

    template <class T>
    T & GPointer<T>::operator *(void)
    {
        if( ptr )
            return (*ptr);
        throw std::runtime_error("错误的NULL指针");
    }

    template <class T>
    const T& GPointer<T>::operator *(void) const
    {
        if( ptr )
            return (*ptr);
        throw std::runtime_error("错误的NULL指针");
    }

    template <class T>
    T * GPointer<T>::operator ->(void)
    {
        if( ptr )
            return ptr;
        else
            return NULL;
    }

    template <class T>
    const T * GPointer<T>::operator ->(void) const
    {
        if( ptr )
            return ptr;
        else
            return NULL;
    }

    template <class T>
    T* GPointer<T>::operator +(unsigned int offset)
    {
        if( ptr )
            return (ptr + offset);
        else
            return NULL;
    }

    template <class T>
    const T* GPointer<T>::operator +(unsigned int offset) const
    {
        if( ptr )
            return ( ptr + offset );
        else
            return NULL;
    }
懒得写注释了,没有多少..应该随便读的懂..
可以像普通指针那样使用,比如:
  1. GPointer<int> ptr;
    *ptr = 10;
    GPointer<double> ptr2 = new double[10];
    *(ptr2+2) = 12.2;
也可以这样,
  1. typedef GPointer<int> pint_t;
    typedef GPointer<long> plong_t;
    typedef GPointer<char> pchar_t;
    // ...
    pint_t ptr;
    *ptr = 12;
    // ...

(原文链接:http://blog.abreto.net/blog/2012/02/smart-pointer.html).


abreto 2012-02-03 13:51
卧渠..超过时间限制..
改了一下
  1. #include <stdexcept>

    /**
    * Author: Abreto <m@abreto.net>
    *
    **/
    template <class T>
    class GPointer
    {
    public:
        GPointer(void);
        GPointer(T);
        GPointer(T *);
        GPointer(const GPointer<T> &);
        ~GPointer();
      
        T & operator *(void);
        const T & operator *(void) const;
        T * operator ->(void);
        const T * operator ->(void) const;
      
        T * operator +(unsigned int);
        const T * operator +(unsigned int) const;
    private:
        T*         ptr;
        size_t*     use;
      
        void inc_use(void);
        void dec_use(void);
    };

    template <class T>
    void GPointer<T>::inc_use(void)
    {
        (*use)++;
    }

    template <class T>
    void GPointer<T>::dec_use(void)
    {
        if( 0 == (--(*use)) )
        {
            delete ptr;
            delete use;
        }
    }

    template <class T>
    GPointer<T>::GPointer(void)
    {
        ptr = new T;
        use = new size_t(0);
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(T d)
    {
        ptr = new T(d);
        use = new size_t(0);
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(T *p)
    {
        ptr = p;
        use = new size_t(0);
      
        inc_use();
    }

    template <class T>
    GPointer<T>::GPointer(const GPointer<T> &p)
    {
        ptr = p.ptr;
        use = p.use;
      
        inc_use();
    }

    template <class T>
    GPointer<T>::~GPointer(void)
    {
        dec_use();
    }

    template <class T>
    T & GPointer<T>::operator *(void)
    {
        if( ptr )
            return (*ptr);
        throw std::runtime_error("错误的NULL指针");
    }

    template <class T>
    const T& GPointer<T>::operator *(void) const
    {
        if( ptr )
            return (*ptr);
        throw std::runtime_error("错误的NULL指针");
    }

    template <class T>
    T * GPointer<T>::operator ->(void)
    {
        if( ptr )
            return ptr;
        else
            return NULL;
    }

    template <class T>
    const T * GPointer<T>::operator ->(void) const
    {
        if( ptr )
            return ptr;
        else
            return NULL;
    }

    template <class T>
    T* GPointer<T>::operator +(unsigned int offset)
    {
        if( ptr )
            return (ptr + offset);
        else
            return NULL;
    }

    template <class T>
    const T* GPointer<T>::operator +(unsigned int offset) const
    {
        if( ptr )
            return ( ptr + offset );
        else
            return NULL;
    }



outman 2012-02-03 14:12
第二层地狱了 AB你把18层全部占光吧

jybox 2012-02-03 15:51
计数的功能好像不大对劲

size_t*     use;这个为毛用动态内存阿,这不是没事给自己找事么

abreto 2012-02-03 20:10
jybox:计数的功能好像不大对劲
size_t*     use;这个为毛用动态内存阿,这不是没事给自己找事么 (2012-02-03 15:51) 

新的指针use+1后而旧的指针的use不会加1(如果不用指针)

jybox 2012-02-03 22:21
abreto:新的指针use+1后而旧的指针的use不会加1(如果不用指针) (2012-02-03 20:10) 

好吧,我2B了

abreto 2012-02-04 10:22
jybox:好吧,我2B了 (2012-02-03 22:21) 


abreto 2012-02-05 11:29




Powered by phpwind v8.7 Code ©2003-2011 phpwind
Time 0.042422 second(s),query:5 Gzip enabled