The original writing, scoped and shared notation of "C + +" smart pointers

Source: Internet
Author: User

Three common ways to do smart pointers:

First, the original writing, the original wording can be understood as the method of pointer transfer.

Template<typename t>class autoptr{public:    autoptr ()          :_ptr (NULL)     {}    autoptr (T* ptr )         :_ptr (PTR)     {}    ~ Autoptr ()     {        if  (_ptr)          {             Delete _ptr;            _ptr = null ;        }    }    autoptr< T> (AUTOPTR&LT;T&GT;&AMP;&NBSP;AP)         : _ptr (ap._ptr)      {        ap._ptr = NULL;     }    autoptr<t>& operator =  (AUTOPTR&LT;T&GT;&AMP;&NBSP;AP)      {        if  (THIS&NBSP;!=&NBSP;&AMP;AP)          {             Delete _ptr;            _ptr = ap._ ptr;            ap._ptr = null;         }        return *this;     }    t& operator* ()     {         return *_ptr;    }    t*  gerptr ()     {        return _ptr;     }private:    t* _ptr;}; 

  Second, the evolution of the later scoped, but also known as the Guard writing. The advantage of this notation in relation to the original writing is that it is not allowed to use the overloading of copy constructs and operators, thus avoiding the problem of pointer-to-depth copies. The procedure is to declare the copy construction, the overloading of the operators, and not define them, and protect them with protected. Scoped notation is a reference to the boost library. Interested can go to understand this thing, behind still have a lot of story, in here I don't say much.

Template<class t>class scopedptr{public:    scopedptr ()          :_ptr (NULL)     {}    scopedptr (T*  PTR)         :_ptr (PTR)     {}     ~scopedptr ()     {        if  (_PTR)         {             delete _ptr;            _ptr  = NULL;        }    }         t& operator* ()     {         return *_ptr;    }    T* operator-> ()     {        return _ptr;    }    t*  getptr ()     {        return _ptr;     }protected: //plus protected prevents users from defining overloaded functions for copy constructs and operators outside of the class     scopedptr <T> (CONST&NBSP;SCOPEDPTR&LT;T&GT;&AMP;&NBSP;SP);         // Do not allow the user to use the copy, can prevent the copy, therefore only declares does not define     scopedptr<t>& operator= (const scopedptr &LT;T&GT;&AMP;&NBSP;SP);p rivate:    t* _ptr;};

Three, the sharedptr wording

This method takes into account the problem of deep-and-dark copy and refers to the reference counter to solve the problem of shallow copy, which realizes the function that the smart pointer wants to realize.

Template<class t>class shareptr{public:    shareptr (T* ptr)          :_ptr (PTR)         , _ Pcount (New int (1))     {}    //shareptr (Shar)      //    :_ptr (sp._ptr)     //{    //     *_pcount = 1;    //}    ~shareptr ()      {        if  (_ptr)          {            if  (--(* _pcount)  == 0)             {                 delete _ptr;                 delete _pcount;                 _ptr = NULL;                 _pCount = NULL;             }             _ptr = NULL;        }     }    SharePtr<T> (CONST&NBSP;SHAREPTR&LT;T&GT;&AMP;&NBSP;SP)      {        _ptr = sp._ptr;         _pcount = sp._pcount;        + + (*_pcount);     }    shareptr<t>& operator= (const  Shareptr<t>&&nBSP;SP)     {        if  (this !=  &AMP;SP)         {             if  (--(*_pcount)  == 0)     //here to know who to subtract one, the logic needs to be analyzed clearly             {                 delete _ptr;                 delete _pCount;                 _ptr = NULL;                 _pCount = NULL;             }            &nBsp;_ptr = sp._ptr;            _pcount  = sp._pcount;            ++ (*_pCount);         }        return * This;    }private:    t* _ptr;    int* _ Pcount;};


This article is from "drip" blog, please be sure to keep this source http://10740329.blog.51cto.com/10730329/1766046

The original writing, scoped and shared notation of "C + +" smart pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.