Analog implementation of smart pointers in C + + standard libraries and boost libraries

Source: Internet
Author: User

We know that smart pointer auto_ptr is defined in the C + + standard library, but we seldom use it, because although it can automatically reclaim dynamically opened memory, it does not require the programmer to maintain the dynamically opened memory, but when it is used to assign value or copy construction, there is a management transfer process, So that we can not be very convenient to use auto_ptr.

The following is a simple implementation of auto_ptr, and we can see that it will transfer management when copying and assigning values.

Template<class t>class autoptr{public:     autoptr (T* ptr)        :_ptr (PTR)      {}     ~autoptr ()      {          if  (_PTR)           {                delete _ptr;           }     }     autoptr (AutoPtr<T>&  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 * operator-> ()      {           return _ptr;     }private:     t* _ptr;}; 

We know that there are smart pointers in the boost library that are implemented in a different way from the C + + standard library.

Scoped_ptr,scoped_array is a smart pointer that is implemented in a very simple and rude way, and it does not allow users to copy and assign the object. Here's how they're implemented:

Template<typename t>class scopedptr{public:     scopedptr (T* ptr )      :_ptr (PTR)      {}     ~ Scopedptr ()      {          if  (_ptr)           {                delete _ptr;           }     }     T* operator-> ()       {          return _ptr;      }     t& operator* ()      {           return *_ptr;     } Protected:  &nbSp;  scopedptr (SCOPEDPTR&LT;T&GT;&AMP;&NBSP;SP);     scopedptr<t> & operator= (SCOPEDPTR&LT;T&GT;&AMP;&NBSP;SP);p rivate:     t* _ptr;};  template<class t>class scopedarray{public:     scopedarray (T*  ptr)       :_ptr (PTR)      {}      ~scopedarray ()      {           if  (_ptr)           {                cout <<  "Delete"   << endl;                delete[] _ptr;          }      }     t&aMp; operator[] (Int index)      {           return _ptr[index];     }protected:      scopedarray (Scopedarray<t>& sparr);     scopedarray&  Operator= (Scopedarray<t>& sparr);p rivate:     t* _ptr;};

The Shared_ptr,shared_array in the Boost library is to manage dynamically opened memory with a pointer and a reference count, creating a space that dynamically opens up a space for reference counting, and recording the number of times the space is referenced.

Unlike auto_ptr, multiple pointers can be allowed to manage a unified empty space.

Template<class t>class sharedptr{public:     sharedptr (T*&NBSP;PTR)       :_ptr (PTR)       , _pcount (new long (1))      {}     sharedptr (const sharedptr<t>& &NBSP;SP)       :_ptr (sp._ptr)       , _pcount ( Sp._pcount)      {          ++ (*_ Pcount);      }     /*sharedptr& operator= (const &NBSP;SHAREDPTR&LT;T&GT;&AMP;&NBSP;SP)//Traditional assignment method      {          if  (_ptr != sp._ptr)           {             release ();              _ptr = sp._ptr;              _pCount = sp._pCount;              ++ (*_pcount);         }          return *this;     }*/      sharedptr& operator= (SHAREDPTR&LT;T&GT;&NBSP;SP)//Modern assignment Method       {          swap (_ptr, sp._ptr);           swap (_pcount, sp._pcount);           return *this;     }     ~ Sharedptr ()      {          release ( ); &NBSP;&NBSP;&NBSP;&NBSp; }     long usecount ()      {           return *_pCount;     }      T* operator-> ()      {           return _ptr;     }     T&  operator* ()      {           Return *_ptr;     }protected:     void release ()      {      if  (--(*_pcount)  == 0)        {       delete _ptr;        delete _pCount;      }      }private:     t* _ptr;     long* _pcount;};   template<class T>class SharedArray{public:      Sharedarray (t* ptr)       :_ptr (PTR)         _pcount (New long (1))      {}     ~sharedarray ()      {      release ();      }      sharedarray (Const sharedarray<t>& sparr)        :_ptr (sparr._ptr)       ,_pcount (spArr._pCount)       {      ++ (*_pcount);     }      /*sharedarray& operator= (Sharedarray<t> sparr)       {      swap (_ptr, sparr._PTR);       swap (_pcount, sparr._pcount);      }*/      sharedarray& operator= (Const sharedarray<t>& sparr)      {      if  (_PTR&NBSP;!=&NBSP;SPARR._PTR)       {           release ();           _ptr = sparr._ptr;            _pCount = spArr._pCount;            ++ (*_pcount);      }       return *this;     }     T&  operator[] (Int index)      {           return&nbsP;_ptr[index];     }private:     void release ()       {          if  (--(*_pcount)  == 0)           {                delete[] _ptr;                delete _pCount;           }     }private:     t* _ ptr;     long* _pcount;};

Analog implementation of smart pointers in C + + standard libraries and boost libraries

Related Article

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.