The following are two auot_ptr implementations. The first version provides the class interface and implements all member functions outside the class definition body. In the second version, all member functions are implemented in the definition body.
In terms of style, the second implementation is not as good as the first one, because it does not separate the class interface from the implementation. But auto_ptr is just a simple class, so the second implementation is much clearer than the first one.
This is the version that separates the auto_ptr interface from the implementation:
Template <class T> <br/> class auto_ptr {<br/> Public: <br/> explicit auto_ptr (T * p = 0 ); // item M5 has a description of "explicitfor" <br/> // <br/> template <Class U> // copy the constructor member template <br/> auto_ptr (auto_ptr <u> & RHs ); // (see item m28 ): <br/> // use another compatible <br/> // auto_ptr object <br/> // initialize a new auto_ptr object <br/> ~ Auto_ptr (); <br/> template <Class U> // value assignment operator member template <br/> auto_ptr <t> & // (see item m28 ): <br/> operator = (auto_ptr <u> & RHs ); // use another compatible <br/> // assign a value to the auto_ptr object <br/> T & operator * () const; // see item m28 <br/> T * operator-> () const; // see item m28 <br/> T * Get () const; // return the inclusive pointer <br/> // current value <br/> T * release (); // discard the inclusive pointer <br/> // ownership, <br/> // and return the current value <br/> void reset (T * p = 0); // Delete the inclusive pointer, <br/> // obtain the position of the pointer P <Br/> PRIVATE: <br/> T * pointee; <br/> template <Class U> // make all auto_ptr classes <br/> friend class auto_ptr <u>; // become a friend <br/> }; </P> <p> template <class T> <br/> inline auto_ptr <t>: auto_ptr (T * P) <br/>: pointee (P) <br/>{}</P> <p> template <class T> <br/> inline auto_ptr <t>: auto_ptr (auto_ptr <u> & RHs) <br/>: pointee (RHS. release () <br/>{}</P> <p> template <class T> <br/> inline auto_ptr <t> ::~ Auto_ptr () <br/> {Delete pointee ;} </P> <p> template <class T> <br/> template <Class U> <br/> inline auto_ptr <t> & auto_ptr <t> :: operator = (auto_ptr <u> & RHs) <br/>{< br/> If (this! = & RHs) reset (RHS. release (); <br/> return * this; <br/>}</P> <p> template <class T> <br/> inline T & auto_ptr <t >:: operator *() const <br/> {return * pointee ;}</P> <p> template <class T> <br/> inline T * auto_ptr <t> :: operator-> () const <br/> {return pointee ;}</P> <p> template <class T> <br/> inline T * auto_ptr <t> :: get () const <br/> {return pointee ;}</P> <p> template <class T> <br/> inline T * auto_ptr <t> :: release () <BR/>{< br/> T * oldpointee = pointee; <br/> pointee = 0; <br/> return oldpointee; <br/>}</P> <p> template <class T> <br/> inline void auto_ptr <t >:: reset (T * P) <br/>{< br/> If (pointee! = P) {<br/> Delete pointee; <br/> pointee = P; <br/>}< br/>
The auto_ptr template defines all functions in the class definition body. As you can see, it is easy to understand:
Template <class T> <br/> class auto_ptr {<br/> Public: <br/> explicit auto_ptr (T * p = 0): pointee (P) {}< br/> template <Class U> <br/> auto_ptr (auto_ptr <u> & RHs): pointee (RHS. release () {}< br/> ~ Auto_ptr () {Delete pointee ;}< br/> template <Class U> <br/> auto_ptr <t> & operator = (auto_ptr <u> & RHs) <br/>{< br/> If (this! = & RHs) reset (RHS. release (); <br/> return * This; <br/>}< br/> T & operator * () const {return * pointee ;} <br/> T * operator-> () const {return pointee ;}< br/> T * Get () const {return pointee ;} <br/> T * release () <br/> {<br/> T * oldpointee = pointee; <br/> pointee = 0; <br/> return oldpointee; <br/>}< br/> void reset (T * p = 0) <br/>{< br/> If (pointee! = P) {<br/> Delete pointee; <br/> pointee = P; <br/>}< br/> PRIVATE: <br/> T * pointee; <br/> template <Class U> friend class auto_ptr <u >;< br/> };
If the compiler you use does not support "Explicit", you can safely use # define to cancel its existence:
# Define explicit
This will not weaken any function of auto_ptr, but cause slight security compromise. For details, see item M5.