VC under the Smart pointer, focusing on ownership of the transfer of # include <iostream>using namespace Std;template<class type>class autoptr{public: autoptr (int *p = null):p TR (P), owns (ptr! = NULL) {}autoptr (const autoptr<type> &t):p tr (t.release ()), owns (T.ow NS) {}autoptr<type>& operator= (const autoptr<type> &t) {if (This! = &t)//Determines whether to assign itself a value of {if (ptr! = T.PTR)//Determine whether to point to the same space {if (owns)//If there is ownership, then free the current space {delete ptr;} Else{owns = t.owns;//Conversely, get ownership}ptr = T.release ();//Let T lose ownership}}return *this;} ~autoptr () {if (owns) delete ptr;} public:type& operator* () Const{return (*get ());} type* operator-> () Const{return get ();} Type* get () Const{return ptr;} type* release () const{((autoptr<type> *const) this)->owns = False;return ptr;} Private:bool owns; Type *ptr;}; int main () {int *p = new int (10); Autoptr<int> PA (p); cout << *pa << Endl; Autoptr<int> PA1 (PA), cout << *pa1 << endl;int *p1 = new int (100); Autoptr<int> PA2 (p1); autoptr<int> pa3;pa3 = Pa2;cout << *pa3 << Endl;return 0;}
"C + +" smart pointer