#include <iostream>using namespace STD;Template<TypeNameType>class auto_ptr{ Public:auto_ptr(Type *d = NULL):p tr (d), Own (D!=null) {}auto_ptr(Const auto_ptr&AP) {ptr = Ap.realse (); own =true; }auto_ptr&operator=(Const auto_ptr&AP) {if( This!=&AP) {if(Ap.own)//ap has ownership rights. {ptr = Ap.realse (); own =true; }Else//ap has no ownership rights.{ptr = Ap.realse (); own =false; } } } ~auto_ptr() {if(Own)Deleteptr } Typeoperator*() {if(Own)return*( This->PTR); } type*operator() {if(Own)returnptr } type* Realse ()Const//Must be const, the constant object can only call the usual method.{if(Own) ((auto_ptr*)( This))->own=false; type* tmp = This->ptr;returntmp }Private:BOOLOwn//ownership, and only one object can have ownership at the same time. Type *ptr;};intMain () {int*p =New int( -);auto_ptr<int> PS (P);auto_ptr<int> PB; PB = PS;cout<<*pb<<endl;return 0;}
C++PJ Smart pointer