C ++ templates implement smart pointers

Source: Internet
Author: User

The following is the smart pointer implemented by the C ++ template.

 /*************************************** * ********************** <Br/> * Name: csmartptr. h <br/> * function: smart pointer <br/> * programmed: ming_zhang <br/> * Date (org): Jun 02 2010 <br/> * Project: <br/> * OS: Windows XP <br/> * history: <br/> * ID --- date ----------- note --------------------------- <br/> * 00 <br/> ******************* **************************************** * ***/</P> <p> # ifndef _ csmartp Tr_h_efbab248_67c2_4c3b_a146_fc03095ec3ba <br/> # DEFINE _ attributes </P> <p> namespace STM <br/>{</P> <p> template <typename T> <br/> class csmartptr <br/>{< br/> public: <br/>/* constructor */<br/> csmartptr (); <br/> csmartptr (const T * PTR ); <br/> csmartptr (const csmartptr <t> & PTR ); <br/>/* assignment operators */<br/> csmartptr <t> & operator = (T * PTR); <br/> csmartptr <t> & Operator = (const csmartptr <t> & PTR); </P> <p> T * operator-> () Throw (runtime_error ); <br/> T & operator * () Throw (runtime_error); <br/>/* const version */<br/> T * const operator-> () const throw (runtime_error); <br/> const T & operator * () const throw (runtime_error); <br/>/* Destructors */<br/> ~ Csmartptr (); <br/> PRIVATE: <br/> int * _ ptrcount; <br/> T * _ PTR; <br/> }; </P> <p> template <typename T> <br/> csmartptr <t>: csmartptr (): _ PTR (0), _ ptrcount (0) <br/>{< br/>}</P> <p> template <typename T> <br/> csmartptr <t>: csmartptr (const T * PTR ): _ PTR (PTR) <br/>{< br/> If (_ PTR! = NULL)/* prevent the parameter from being null */<br/>{< br/> _ ptrcount = new int (0); <br/> (* _ ptrcount) ++; <br/>}</P> <p> template <typename T> <br/> csmartptr <t> :: csmartptr (const csmartptr <t> & PTR): _ PTR (0), _ ptrcount (0) <br/>{< br/> If (PTR. _ PTR! = 0)/* prevent the parameter from being a null pointer */<br/>{< br/> This-> _ PTR = PTR. _ PTR; <br/> This-> _ ptrcount = PTR. _ ptrcount; <br/> (* _ ptrcount) ++; <br/>}< br/> template <typename T> <br/> csmartptr <t> ::~ Csmartptr () <br/>{< br/> If (_ PTR! = 0) <br/>{< br/> (* _ ptrcount) --; <br/> If (* _ ptrcount = 0) <br/>{< br/> Delete _ PTR; <br/> Delete _ ptrcount; <br/>}</P> <p> template <typename T> <br/> csmartptr <t> & csmartptr <t>:: Operator = (T * PTR) <br/>{< br/> If (PTR! = This-> _ PTR) <br/> {// prevent self-assignment <br/> If (_ PTR = NULL) <br/> {// empty <br/> If (PTR! = NULL) // The parameter is not null <br/>{< br/> _ ptrcount = new int (0); <br/> (* _ ptrcount) ++; <br/> _ PTR = PTR; <br/>}< br/> else // The parameter is null <br/>{< br/>}< br/> else <br/> {// you are not empty. <br/> (* _ ptrcount) --; <br/> If (* _ ptrcount = 0) <br/> {/* release _ PTR */<br/> Delete _ PTR; <br/>}< br/> _ PTR = PTR; <br/> * _ ptrcount = 1; <br/>}< br/> return * this; <br/>}</P> <p> template <typename T> <br/> csmartptr <t> & csmartptr <t>: Operator = (const C Smartptr <t> & PTR) <br/> {<br/> If (PTR. _ PTR! = _ PTR) <br/>{// prevent self-assignment <br/> If (PTR. _ PTR = 0) <br/>{// the parameter is null <br/> If (_ PTR! = 0) <br/> {// you are not empty <br/> (* _ ptrcount) --; <br/> If (* _ ptrcount = 0) <br/> {// release the space originally pointed to <br/> Delete _ PTR; <br/> Delete _ ptrcount; <br/>}< br/> _ PTR = PTR. _ PTR; <br/> _ ptrcount = PTR. _ ptrcount; <br/>}< br/> else <br/>{// do nothing if you are empty <br/>}< br/> else <br />{// the parameter is not blank <br/> If (_ PTR! = 0) <br/> {// It is not empty <br/> (* _ ptrcount) --; <br/> If (* _ ptrcount = 0) <br/> {// release the space originally pointed to <br/> Delete _ PTR; <br/> Delete _ ptrcount; <br/>}< br/> _ PTR = PTR. _ PTR; <br/> _ ptrcount = PTR. _ ptrcount; <br/> (* _ ptrcount) ++; <br/>}< br/> return * this; <br/>}</P> <p> template <typename T> <br/> T * csmartptr <t >:: operator-> () Throw (runtime_error) <br/>{< br/> If (_ PTR! = 0) <br/> return _ PTR; <br/> throw runtime_error ("access through Null Pointer "); <br/>}</P> <p> template <typename T> <br/> T & csmartptr <t>: Operator * () Throw (runtime_error) <br/>{< br/> If (_ PTR! = 0) <br/> return * _ PTR; <br/> throw STD: runtime_error ("dereference of Null Pointer "); <br/>}</P> <p> template <typename T> <br/> T * const csmartptr <t >:: operator-> () const throw (runtime_error) <br/>{< br/> If (_ PTR! = 0) <br/> return _ PTR; <br/> throw runtime_error ("access through Null Pointer "); <br/>}< br/> template <typename T> <br/> const T & csmartptr <t>: Operator * () const throw (runtime_error) <br/>{< br/> If (_ PTR! = 0) <br/> return * _ PTR; <br/> throw STD: runtime_error ("dereference of Null Pointer "); <br/>}</P> <p >}// namespace STM <br/> # endif/_ csmartptr_h_efbab248_67c2_4c3b_a146_fc03095ec3ba

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.