Analysis of Auto_ptr source code under VC and Linux

Source: Internet
Author: User

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/87/03/wKioL1fRPwHRs2w_AAAVLNuRHXM913.png-wh_500x0-wm_3 -wmp_4-s_4156505165.png "title=" VC under the transfer of management rights "alt=" Wkiol1frpwhrs2w_aaavlnurhxm913.png-wh_50 "/>

1.vctemplate<class _ty>class auto_ptr{public:typedef _ty element_type;auto_ptr (_Ty  *_p = 0)  : _owns (_p != 0),  _ptr (_p) {}//copy construction    _y management _Owns  = false  after release, assign the pointer to the past auto_ptr (const auto_ptr<_ty>& _y)  : _owns (_y._ owns),  _ptr (_y.release ()) {}auto_ptr<_ty>& operator= (const auto_ptr<_ty>&  _y)  _throw0 () {if (this != &_y)//Prevent Word assignment {   if  (_ptr != _ Y.get ())    {   if  (_owns) delete _ptr;//when owns is true, release the space it refers to as Ptr     _Owns = _Y._Owns;    }   else if  (_y._owns)  _owns = true;   _ptr = _y.release ();//}return  (*this);  }~ Auto_ptr () {///The destructor is called at the end of the program, freeing Space if  (_owns) delete _ptr;} Overload * After having a similar access form to the pointer _ty& operator* ()  const _throw0 () {return  (*get ());} _ty *operator-> ()  const _throw0 () { return  (get ());  }_ty *get ()   Const _throw0 () {return  (_ptr);} _ty *release ()  const _throw0 ()//Just set the owns to false without changing the pointer to {((auto_ptr<_ty> *) this) _owns = false;return  (_ptr); }private:bool _owns;_ty *_ptr;}; Int main () {    int *p =  (int *) malloc (sizeof (int)); *p =  3;        int *q =  (int *) malloc (sizeof (int) ); *q = 5;auto_ptr<int> pa (P); AUTO_PTR&LT;INT&GT;&NBSP;PB (q);p a = pb;cout< <*pa<<endl;//at this time the output is 5cout<<*pb<<endl;//output 5    return 0;} such as 2.linux
template<class _ty>class auto_ptr{       public:                 explicit auto_ PTR (_ty *p = 0): _m_ptr (P) {}~auto_ptr () {    delete _m_ptr;} Auto_ptr (auto_ptr<_ty> &_y): _m_ptr (_y.release ()) {}_ty& operator * () {     return *_m_ptr;} _ty& operator -> () {    return  (*this)->_M_Ptr;} auto_ptr<_ty> &operator = (auto_ptr<_ty> & _y)//Difference {                     reset (_ Y.release ());       return *this;} _ty* release () {                      _ty *_tmp = _M_Ptr;                      _m_ptr = 0;//linux the pointer to null, if Access transfer permission     return  _tmp;} Void reset (_ty *_ptr = 0)//Reset the management of the pointer {   _ty * _pt = _m_ ptr;                    if (_ptr!=_pt)    {delete _pt;//Delete the space of the front pointer first to avoid a memory leak    }                    _m _ptr = _ptr;} private:_ty* _m_ptr;//and VC under different is no permission flag};

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/87/03/wKioL1fRRfqiR9b_AAArKzU3bck981.png-wh_500x0-wm_3 -wmp_4-s_320113280.png "title=" Untitled 2.png "alt=" Wkiol1frrfqir9b_aaarkzu3bck981.png-wh_50 "/>

PB even the opportunity to access also lost .....



If there are errors, welcome the predecessors to guide, hey.

Analysis of Auto_ptr source code under VC and Linux

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.