文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>代码剖析:<memory>中的智能指针auto_ptr

代码剖析:<memory>中的智能指针auto_ptr

时间:2010-10-29  来源:轻轻听

人之所以区别于动物就在这里体现出来了,于是就有了智能指针auto_ptr。

智能指针不是全能的,它也是有缺陷的,不过我这里只是对标准库中的auto_ptr进行代码剖析,但是本人功底是有限的,大家可以抛砖抛鸡蛋抛鲜花,我都不会介意的。

 

先奉上代码,代码就是程序的肉体,思想就是程序的灵魂。

我们看到智能指针的肉体后,也要对它的灵魂进行了解,不能被外表所迷惑。

 

 // TEMPLATE CLASS auto_ptr
template<class _Ty>
 class auto_ptr;

template<class _Ty>
 struct auto_ptr_ref
  { // proxy reference for auto_ptr copying
 explicit auto_ptr_ref(_Ty *_Right)                                                    explicit防止隐式使用拷贝构造函数
  : _Ref(_Right)
  { // construct from generic pointer to auto_ptr ptr
  }

 _Ty *_Ref; // generic pointer to auto_ptr ptr
 };

template<class _Ty>
 class auto_ptr
  { // wrap an object pointer to ensure destruction
public:
 typedef _Ty element_type;

 explicit auto_ptr(_Ty *_Ptr = 0) _THROW0()                                          explicit防止隐式使用拷贝构造函数
  : _Myptr(_Ptr)
  { // construct from object pointer
  }

 auto_ptr(auto_ptr<_Ty>& _Right) _THROW0()
  : _Myptr(_Right.release())
  { // construct by assuming pointer from _Right auto_ptr
  }

 auto_ptr(auto_ptr_ref<_Ty> _Right) _THROW0()
  { // construct by assuming pointer from _Right auto_ptr_ref
  _Ty *_Ptr = _Right._Ref;
  _Right._Ref = 0; // release old
  _Myptr = _Ptr; // reset this
  }

 template<class _Other>
  operator auto_ptr<_Other>() _THROW0()
  { // convert to compatible auto_ptr
  return (auto_ptr<_Other>(*this));
  }

 template<class _Other>
  operator auto_ptr_ref<_Other>() _THROW0()
  { // convert to compatible auto_ptr_ref
  _Other *_Cvtptr = _Myptr; // test implicit conversion
  auto_ptr_ref<_Other> _Ans(_Cvtptr);
  _Myptr = 0; // pass ownership to auto_ptr_ref
  return (_Ans);
  }


 template<class _Other>
  auto_ptr<_Ty>& operator=(auto_ptr<_Other>& _Right) _THROW0()
  { // assign compatible _Right (assume pointer)
  reset(_Right.release());
  return (*this);
  }

 template<class _Other>
  auto_ptr(auto_ptr<_Other>& _Right) _THROW0()
  : _Myptr(_Right.release())
  { // construct by assuming pointer from _Right
  }

 auto_ptr<_Ty>& operator=(auto_ptr<_Ty>& _Right) _THROW0()
  { // assign compatible _Right (assume pointer)
  reset(_Right.release());
  return (*this);
  }

 auto_ptr<_Ty>& operator=(auto_ptr_ref<_Ty> _Right) _THROW0()
  { // assign compatible _Right._Ref (assume pointer)
  _Ty *_Ptr = _Right._Ref;
  _Right._Ref = 0; // release old
  reset(_Ptr); // set new
  return (*this);
  }

 ~auto_ptr()
  { // destroy the object
  delete _Myptr;                                                                                    //析构函数释放内存
  }

 _Ty& operator*() const _THROW0()
  { // return designated value

 #if _HAS_ITERATOR_DEBUGGING
  if (_Myptr == 0)
   _DEBUG_ERROR("auto_ptr not dereferencable");
 #endif /* _HAS_ITERATOR_DEBUGGING */

  __analysis_assume(_Myptr);

  return (*get());
  }

 _Ty *operator->() const _THROW0()
  { // return pointer to class object

 #if _HAS_ITERATOR_DEBUGGING
  if (_Myptr == 0)
   _DEBUG_ERROR("auto_ptr not dereferencable");
 #endif /* _HAS_ITERATOR_DEBUGGING */

  return (get());
  }

 _Ty *get() const _THROW0()
  { // return wrapped pointer                                                         get()返回指针
  return (_Myptr);
  }

 _Ty *release() _THROW0()
  { // return wrapped pointer and give up ownership                        release()返回指针,放弃所有权                 
  _Ty *_Tmp = _Myptr;
  _Myptr = 0;
  return (_Tmp);
  }

 void reset(_Ty* _Ptr = 0)
  { // destroy designated object and store new pointer                      reset()销毁指定对象,存储新指针
  if (_Ptr != _Myptr)
   delete _Myptr;
  _Myptr = _Ptr;
  }

private:
 _Ty *_Myptr; // the wrapped object pointer
 };

 

我所了解的智能指针的灵魂:

在于构造栈上对象的生命期控制堆上构造的对象的生命期。因为在智能指针的内部,存储着堆对象的指针,而且在构析函数中调用delete行为。

 

 

华丽分割线===================================================================

轻轻听综合圣经软件: http://www.qingqingting.com/

迦南旺铺: http://KanGuoLai.taobao.com/

 

 

相关阅读 更多 +
排行榜 更多 +
我狙击打的贼准

我狙击打的贼准

飞行射击 下载
枪战突击

枪战突击

飞行射击 下载
其乐无穷

其乐无穷

飞行射击 下载