静心学习之路系列

静心学习之路(6)——C++ auto_ptr的实现

2020-11-16  本文已影响0人  游学者夏纳

Source Code:《More Effective C++》
关键词:explicit, member template, assinment operator

template<class T>
class auto_ptr {
public:
  explicit auto_ptr(T *p = 0);              // Item M5 有“explicitfor”
                                            // 的描述
  template<class U>                         // 拷贝构造函数成员模板
  auto_ptr(auto_ptr<U>& rhs);               //  (见Item M28):
                                            // 用另一个类型兼容的
                                            // auto_ptr对象
                                            // 初始化一个新的auto_ptr对象
  ~auto_ptr();
  template<class U>                         // 赋值操作成员模板
  auto_ptr<T>&                              // (见Item M28):
  operator=(auto_ptr<U>& rhs);              // 用另一个类型兼容的
                                            // auto_ptr对象给它赋值
  T& operator*() const;                     // 见Item M28
  T* operator->() const;                    // 见Item M28
  T* get() const;                           // 返回包容指针的
                                            // 当前值
  T* release();                             // 放弃包容指针的
                                            // 所有权,
                                            // 并返回其当前值
  void reset(T *p = 0);                     // 删除包容指针,
                                            // 获得指针p的所有权
private:
  T *pointee;
template<class U>                           // 让所有的auto_ptr类
friend class auto_ptr<U>;                   // 成为友元
};

template<class T>
inline auto_ptr<T>::auto_ptr(T *p)
: pointee(p)
{}

template<class T>
  inline auto_ptr<T>::auto_ptr(auto_ptr<U>& rhs)
  : pointee(rhs.release())
  {}

template<class T>
inline auto_ptr<T>::~auto_ptr()
{ delete pointee; }

template<class T>
  template<class U>
  inline auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<U>& rhs)
  {
    if (this != &rhs) reset(rhs.release());
    return *this;
  }

template<class T>
inline T& auto_ptr<T>::operator*() const
{ return *pointee; }

template<class T>
inline T* auto_ptr<T>::operator->() const
{ return pointee; }

template<class T>
inline T* auto_ptr<T>::get() const
{ return pointee; }

template<class T>
inline T* auto_ptr<T>::release()
{
  T *oldPointee = pointee;
  pointee = 0;
  return oldPointee;
}

template<class T>
inline void auto_ptr<T>::reset(T *p)
{
  if (pointee != p) {
    delete pointee;
    pointee = p;
  }
}

class写法的版本(风格推荐1,可读性推荐2)

template<class T>
class auto_ptr {
public:
  explicit auto_ptr(T *p = 0): pointee(p) {}
  template<class U>
  auto_ptr(auto_ptr<U>& rhs): pointee(rhs.release()) {}
  ~auto_ptr() { delete pointee; }
  template<class U>
  auto_ptr<T>& operator=(auto_ptr<U>& rhs)
  {
    if (this != &rhs) reset(rhs.release());
    return *this;
  }
  T& operator*() const { return *pointee; }
  T* operator->() const { return pointee; }
  T* get() const { return pointee; }
  T* release()
  {
    T *oldPointee = pointee;
    pointee = 0;
    return oldPointee;
  }
  void reset(T *p = 0)
  {
    if (pointee != p) {
      delete pointee;
      pointee = p;
    }
  }
  private:
    T *pointee;
  template<class U> friend class auto_ptr<U>;
  };
上一篇 下一篇

猜你喜欢

热点阅读