scoped_ptr

 

template <class C>

class scoped_ptr {

 public:

 

  // The element type

  typedef C element_type;

 

  // Constructor.  Defaults to intializing with NULL.

  // There is no way to create an uninitialized scoped_ptr.

  // The input parameter must be allocated with new.

  explicit scoped_ptr(C* p = NULL) : ptr_(p) { }

 

  // Destructor.  If there is a C object, delete it.

  // We don't need to test ptr_ == NULL because C++ does that for us.

  ~scoped_ptr() {

    enum { type_must_be_complete = sizeof(C) };

    delete ptr_;

  }

 

  // Reset.  Deletes the current owned object, if any.

  // Then takes ownership of a new object, if given.

  // this->reset(this->get()) works.

  void reset(C* p = NULL) {

    if (p != ptr_) {

      enum { type_must_be_complete = sizeof(C) };

      delete ptr_;

      ptr_ = p;

    }

  }

 

  // Accessors to get the owned object.

  // operator* and operator-> will assert() if there is no current object.

  C& operator*() const {

    assert(ptr_ != NULL);

    return *ptr_;

  }

  C* operator->() const  {

    assert(ptr_ != NULL);

    return ptr_;

  }

  C* get() const { return ptr_; }

 

  // Comparison operators.

  // These return whether two scoped_ptr refer to the same object, not just to

  // two different but equal objects.

  bool operator==(C* p) const { return ptr_ == p; }

  bool operator!=(C* p) const { return ptr_ != p; }

 

  // Swap two scoped pointers.

  void swap(scoped_ptr& p2) {

    C* tmp = ptr_;

    ptr_ = p2.ptr_;

    p2.ptr_ = tmp;

  }

 

  // Release a pointer.

  // The return value is the current pointer held by this object.

  // If this object holds a NULL pointer, the return value is NULL.

  // After this operation, this object will hold a NULL pointer,

  // and will not own the object any more.

  C* release() {

    C* retVal = ptr_;

    ptr_ = NULL;

    return retVal;

  }

 

 private:

  C* ptr_;

 

  // Forbid comparison of scoped_ptr types.  If C2 != C, it totally doesn't

  // make sense, and if C2 == C, it still doesn't make sense because you should

  // never have the same object owned by two different scoped_ptrs.

  template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;

  template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;

 

  // Disallow evil constructors

  scoped_ptr(const scoped_ptr&);

  void operator=(const scoped_ptr&);

};

 

// Free functions

template <class C>

void swap(scoped_ptr<C>& p1, scoped_ptr<C>& p2) {

  p1.swap(p2);

}

 

template <class C>

bool operator==(C* p1, const scoped_ptr<C>& p2) {

  return p1 == p2.get();

}

 

template <class C>

bool operator!=(C* p1, const scoped_ptr<C>& p2) {

  return p1 != p2.get();

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值