chrome阅读随手记(1) 智能指针实现的一些细节

来源:互联网 发布:js原生日期选择器 编辑:程序博客网 时间:2024/06/07 16:54
template <class C>class scoped_ptr { public:  // The element type  typedef C element_type;  // Constructor.  Defaults to initializing 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) };//注释1      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() WARN_UNUSED_RESULT {    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//注释2  scoped_ptr(const scoped_ptr&);  void operator=(const scoped_ptr&);};


注释1:对incomplete type做检查,防止delete错误。

注释2:如注释,抛弃诟病颇多的智能指针所属权转移问题

话说怎么在csdn贴的代码里加注解呢