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<T>&&NBSP;AP) { if (THIS&NBSP;!=&NBSP;&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<T>&&NBSP;SP); scopedptr<t> & operator= (SCOPEDPTR<T>&&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<T>&&NBSP;SP)//Traditional assignment method { if (_ptr != sp._ptr) { release (); _ptr = sp._ptr; _pCount = sp._pCount; ++ (*_pcount); } return *this; }*/ sharedptr& operator= (SHAREDPTR<T>&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