一个C++中智能指针的设计
来源:互联网 发布:mac adobe cc 安装失败 编辑:程序博客网 时间:2024/04/29 02:45
在C++中,智能指针是存储一些动态分配对象或者资源的类,主要用于控制资源或者动态对象的使用生存期,设计的目的如下:
- 保证能够正确的分配和销毁动态对象或者资源,以防止内存泄露等问题。
- 跟踪对象或者资源的使用情况。
智能指针的实现一般都是使用引用计数,将一个计数器与使用的指针相关联起来,此时引用计数器跟踪该所属类有外部多少共享。因此在实现的时候,就有两个根本的部分
- 计数表示。用于实现对动态对象或者资源使用的计数。
- 指针表示。用于将动态对象或者资源的指针使用间接表现。
根据智能指针主要是上面两大部分,智能指针可以称为“智能计数指针”,智能主要是计数的意思,当然计数的用途就因应用而不同了,或者只是为了跟踪,或者是为了资源管理,或者是为了防止多次释放等等。
一,智能指针实现
下面的模板类,用于实现对指针的封装,其实现的功能如下:
- 指针构造。根据需要被封装的动态对象或者资源的指针,构造智能指针,一般在构造时,会将资源的计数加1;
- 指针运算符重载。下面重载了*,=,->等运算符。
- 指针数据。一个指向模板类型的指针,T* ptr_
template <class T>class scoped_refptr { public: scoped_refptr() : ptr_(NULL) { } scoped_refptr(T* p) : ptr_(p) { if (ptr_) ptr_->AddRef(); } scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) { if (ptr_) ptr_->AddRef(); } template <typename U> scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) { if (ptr_) ptr_->AddRef(); } ~scoped_refptr() { if (ptr_) ptr_->Release(); } T* get() const { return ptr_; } operator T*() const { return ptr_; } T* operator->() const { return ptr_; } T* release() { T* retVal = ptr_; ptr_ = NULL; return retVal; } scoped_refptr<T>& operator=(T* p) { // AddRef first so that self assignment should work if (p) p->AddRef(); if (ptr_ ) ptr_ ->Release(); ptr_ = p; return *this; } scoped_refptr<T>& operator=(const scoped_refptr<T>& r) { return *this = r.ptr_; } template <typename U> scoped_refptr<T>& operator=(const scoped_refptr<U>& r) { return *this = r.get(); } void swap(T** pp) { T* p = ptr_; ptr_ = *pp; *pp = p; } void swap(scoped_refptr<T>& r) { swap(&r.ptr_); } protected: T* ptr_;};有了这个类之后,我们可以定义一个指针,如针对class window的智能指针
scoped_refptr<window> win;此时,上面的模板就会包含一个window *ptr_的指针,从上面可以看出,为了能够正常工作,这类型的指针都必须要实现AddRef和Release方法,这应该不会是要求在class window中实现的吧?那也不太不符合封装的正常逻辑了。答案是:当然不会在class window中实现,这两个方法主要是针对计数的方法,专门针对class window封装一个计数器类,下面的计数器封装
二,计数器的封装
这个类很简单,有三个地方需要注意
- 类从模板类型继承,就是从T继承。上面的class window的话,就是RefCountedObject是window的派生类。
- 封装计数器ref_count_。需要注意的是,对计数器的加减操作应该尽可能的保持原子性。
- 计数器类提供多个构造函数,都是对传入的类型T的多种构造支持。
template <class T>class RefCountedObject : public T { public: RefCountedObject() : ref_count_(0) { } template<typename P> explicit RefCountedObject(P p) : T(p), ref_count_(0) { } template<typename P1, typename P2> RefCountedObject(P1 p1, P2 p2) : T(p1, p2), ref_count_(0) { } template<typename P1, typename P2, typename P3> RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), ref_count_(0) { } template<typename P1, typename P2, typename P3, typename P4> RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4) : T(p1, p2, p3, p4), ref_count_(0) { } template<typename P1, typename P2, typename P3, typename P4, typename P5> RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4, P5 p5) : T(p1, p2, p3, p4, p5), ref_count_(0) { } virtual ~RefCountedObject() { } virtual int AddRef() { return Increment(&ref_count_); } virtual int Release() { int count = Decrement(&ref_count_); if (!count) { delete this; } return count; } protected: int ref_count_;};
三,实例
scoped_refptr<Window> win(new RefCountedObject<Window>(&client, &wnd));上面的实例定义了一个智能指针win,式中client和wnd为其它参数,与定义无关。
- 一个C++中智能指针的设计
- 设计一个智能指针
- C++(智能指针的设计与实现)
- 【C++】智能指针的一个典型应用
- 智能指针的设计
- 智能指针的设计思想
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用 .
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用 .
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- C++中智能指针的设计和使用
- Quest 3d中Lua script中常用方法2
- java代码删除数据库中树形结构数据
- mysql root密码忘掉
- CentOS系统设置和LAMP
- CString转string在unicode的情况下方法不同
- 一个C++中智能指针的设计
- 两种内存碎片
- 计算给定两点的法向量
- cookie的操作
- Ad hoc update to system catalogs is not supported. 解决方案
- 【web】赵雅智_元数据
- Javascript模块化编程(三):require.js的用法
- linux下c++获取进程号
- i.mx53 nand flash partitions