代码剖析:

c++没有垃圾回收机制GC,这是c++的弱项也是c++的强项。就像盲人一样,它的听觉总是比一般正常人好的多(这个例子不是很好,有点贬低了c++,但是不是我的原意)。

人之所以区别于动物就在这里体现出来了,于是就有了智能指针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/

 

 

原文链接: https://www.cnblogs.com/qqting/archive/2010/10/29/1864269.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    代码剖析:

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/16769

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午5:09
下一篇 2023年2月7日 下午5:09

相关推荐