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贴的代码里加注解呢
- chrome阅读随手记(1) 智能指针实现的一些细节
- 随手记——(细节)1
- chrome-智能指针的用法
- 14.智能(smart)指针,实现细节
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 一些定义,随手记的
- 智能指针的一些机制
- 智能指针的一些事儿
- caffe代码阅读1:blob的实现细节-2016.3.14
- caffe代码阅读1:Layer的介绍与实现细节
- C++ (STL BOOST) 智能指针的一些解析
- 智能指针的简单实现
- 智能指针的类实现
- C++ 智能指针的实现
- 以Dialog方式显示activity
- hdu3032 Nim or not Nim?
- Hawk浩客 K390 硬盘易驱线试用
- 解决LaTeX中文输出问题,Ubuntu安装支持中文的LaTeX与其工作环境 ( Texmaker xelatex xecjk )
- 假Sleep函数
- chrome阅读随手记(1) 智能指针实现的一些细节
- 在用hibernate注解来进行配置关系的时候要注意
- VC++MFC 窗口最大化、最小化的实现!
- USEST OJ 1307 - windy数 数位DP
- Oracle index学习总结
- C# 通过PKCS#10证书申请,生成一个cer格式的设备证书
- 如何复制道客巴巴文档
- Maven--生命周期和插件(四)
- TCP/IP协议 三次握手与四次挥手