C + + standard library of simple intelligent pointer auto_ptr simulation to achieve the source __c++

Source: Internet
Author: User

#ifndef _smart_ptr_h #define _SMART_PTR_H template <class t> class Csmartptr {private:template <class y> CLA SS Csmartptrref {private:y * m_py; public:csmartptrref (Y * p): M_py (P) {} Y * get () const throw ();}; T * M_PT; Public:/* Constructor/Explicit csmartptr (T * p = NULL); /* Copy Constructor/* CSMARTPTR (Csmartptr & x); /* Support temporary object conversion to Csmartptrref type can invoke Csmartptr copy constructor/* Resolve temporary object cannot bind to very reference/csmartptr (const csmartptrref<t> & x); /* Assignment operator overload */csmartptr<t> & operator = (csmartptr<t> & s) throw (); /* Support temporary object conversion to CSMARTPTRREF type can be assigned to CSMARTPTR */* Resolve temporary objects cannot be bound to very reference * * Template <class y> csmartptr<t> & Opera Tor = (csmartptrref<y> & s) throw (); * * Different types of smart pointers can be assigned to each other/template <class y> csmartptr<t> & operator = (csmartptr<y> & s) throw (); /* user-defined type conversion/* Resolve temporary objects cannot be bound to very reference * * Template <class z> operator csmartptrref<z> () throw (); /* Obtain the internal pointer value of the smart pointer/T * GET () const throw (); /* Resets the object that the smart pointer points to/void RESET (T * p) throw (); /* Obtain the internal pointer value of the smart pointer, and the smart pointer discards ownership */T * Release () throw (); /* Resolve Address (*) operator overload */T & operator * () const throw (); /* Member access (->) operator overload */T * operator-> () const throw (); /* destructor/* ~CSMARTPTR (); }; /* Constructor/* Template <class t> csmartptr<t>::csmartptr (T * p): M_pt (P) {}/* Support temporary object conversion to Csmartptrref type can invoke CSMARTPT R copy Constructor/* Resolve temporary object cannot be bound to very reference * * Template <class t> csmartptr<t>::csmartptr (const csmartptrref<t> & x) {m_pt = x.m_py}///user-defined type conversion/* Resolve temporary object cannot be bound to very reference * * Template <class t> template <class-z> Csmart Ptr<t>::operator csmartptrref<z> () throw () {return csmartptrref<y> (Release ());}/* Inline template class Csmartptrref member function get/template <class t> template <class y> Y * csmartptr<t>::csmartptrref<y >::get () const throw () {return m_py}//* copy constructor/* Template <class t> (Csmartptr<t>::csmartptr & Amp X) {m_pt = X.release ();/* Assignment operator overload */Template <class t> CsmarTptr<t> & csmartptr<t>::operator = (csmartptr<t> & s) throw () {if (this!= &s) {Delete m_p T M_pt = S.release (); return *this; /* * Support temporary object conversion to CSMARTPTRREF type can be assigned to CSMARTPTR/* Resolve temporary objects cannot be bound to very references * * Template <class t> template <class y> Rtptr<t> & csmartptr<t>::operator = (csmartptrref<y> & s) throw () {if (S.get ()!= m_pt) {delet e m_pt; M_pt = S.get (); return *this; }/* Different types of smart pointers can be assigned to each other * * Template <class t> template <class y> csmartptr<t> & Csmartptr<t>::op Erator = (csmartptr<y> & s) throw () {if (s.m_pt!= m_pt) {delete m_pt; m_pt = S.release ();} return *this;}/ * Get the smart pointer internal pointer value/template <class t> T * csmartptr<t>::get () const throw () {return m_pt;}/* Reset the object to which the smart pointer is pointing */t Emplate <class t> void Csmartptr<t>::reset (T * p) throw () {if (P!= get ()) {delete m_pt; m_pt = P;}}/* Smart pointer internal pointer value, and the smart pointer discards ownership/template <class t> T * csmartptr<T>::release () throw () {T * p = m_pt; m_pt = NULL; return p}/* Resolve address (*) operator overload * * Template <class t> T & CSma Rtptr<t>::operator * () const throw () {return *M_PT}/* member access (->) operator overload */Template <class t> T * csmartpt R<t>::operator-> () const throw () {return m_pt;}/* destructor/template <class t> Csmartptr<t>::~csma Rtptr () {delete m_pt; m_pt = NULL;} #endif

2009-03-06

10:29:35

Shenzhen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.