Simple implementation of the "C + +" smart pointer auto_ptr # include <iostream>using namespace std;template <class _ty>class auto_ptr{public : auto_ptr (_ty *_p = 0): _owns (_ptr! = 0), _ptr (_p) {}auto_ptr<_ty> (const auto_ptr <_Ty> &p): _owns (p._owns ), _ptr (P.release ())//Copy Construction {}auto_ptr<_ty>& operator= (const auto_ptr<_ty> &_y) The assignment Statement {if (this = &_y) {if (_ptr! = _y._ptr) {if (_owns) Delete _ptr;_owns = _y._owns;} else{_owns = _y._owns;} _ptr = _y.release ();} return *this;} ~auto_ptr () {if (_owns) {delete _ptr;}} public:_ty* release () const//Interchange Owner {((auto_ptr<_ty>*const) this)->_owns = False;return _ptr;} _ty*get () Const{return _ptr;} _ty& operator* () Const{return (*get ());} _ty* operator-> () Const{return get ();} Private:_ty *_ptr;bool _owns;}; int main () {int *p = new int (TEN);auto_ptr<int> PA (P);auto_ptr<int> PA1 (PA);auto_ptr<int> Pa2=pa1;cout <<*pa<<endl;cout<<*pa1<<endl;cout<<*pa2<<enDl;return 0;}
Simple implementation of "C + +" smart pointer auto_ptr