智能指針源碼

template <class _Tp> class auto_ptr_test
{
private:
_Tp* _M_ptr
public:
typedef _Tp element_type;ide

explicit auto_ptr_test(_Tp* __p = 0)  : _M_ptr(__p) {}
   auto_ptr_test(auto_ptr_test& __a) : _M_ptr(__a.release()) {}

   auto_ptr_test& operator=(auto_ptr_test& __a) 
   {
     if (&__a != this) 
       {
         delete _M_ptr;
         _M_ptr = __a.release();
     }
     return *this;
 }

   _Tp& operator*() const 
   {
     return *_M_ptr;
 }

    _Tp* operator->() const 
    {
      return _M_ptr;
  }

    _Tp* get() const 
   {
      return _M_ptr;
 }

   _Tp* release()  
  {
      _Tp* __tmp = _M_ptr;
      _M_ptr = 0;
      return __tmp;
}

~auto_ptr_test() { delete _M_ptr; }

};this

相關文章
相關標籤/搜索