谷歌的C++智能指针实现
//智能指针基类所有智能指针对象都继承该类
class RefCountedBase
{
public:
virtual int AddRef()=;
virtual int Release()=;
protected:
virtual ~RefCountedBase(){}
};
智能指针对象类的实现
template <class T>
class RefCountedPtr
{
public:
RefCountedPtr() : m_ptr(NULL)
{
} RefCountedPtr(T* p) : m_ptr(p)
{
if (m_ptr)m_ptr->AddRef();
} RefCountedPtr(const RefCountedPtr<T>& r) : m_ptr(r.m_ptr)
{
if (m_ptr)m_ptr->AddRef();
} template <typename U>
RefCountedPtr(const RefCountedPtr<U>& r) : m_ptr(r.get())
{
if (m_ptr) m_ptr->AddRef();
} ~RefCountedPtr()
{
if (m_ptr) m_ptr->Release();
} T* get() const { return m_ptr; }
operator T*() const { return m_ptr; }
T* operator->() const { return m_ptr; } T* release()
{
T* retVal = m_ptr;
m_ptr = NULL;
return retVal;
} RefCountedPtr<T>& operator=(T* p)
{
// AddRef first so that self assignment should work
if (p) p->AddRef();
if (m_ptr) m_ptr ->Release();
m_ptr = p;
return *this;
} RefCountedPtr<T>& operator=(const RefCountedPtr<T>& r)
{
return *this = r.m_ptr;
} template <typename U>
RefCountedPtr<T>& operator=(const RefCountedPtr<U>& r)
{
return *this = r.get();
} void swap(T** pp)
{
T* p = m_ptr;
m_ptr = *pp;
*pp = p;
} void swap(RefCountedPtr<T>& r)
{
swap(&r.m_ptr);
} protected:
T* m_ptr;
};
使用示例如下:
class MyClass:public RefCountedBase
{
public:
... virtual int AddRef()
{
return ++m_count;
} virtual int Release()
{
int count = --m_count;
if (!count)
{
delete this;
}
return count;
} private: int m_count;
}; RefCountedPtr<MyClass> test=new RefCountedObject<MyClass>()
使用C++模板可以省去每创建一个对象都要实现AddRef() 及 Release() 接口的麻烦
template <class T>
class RefCountedObject : public T
{
public:
RefCountedObject() : m_count()
{
} template<typename P>
explicit RefCountedObject(P p) : T(p), m_count()
{
} template<typename P1, typename P2>
RefCountedObject(P1 p1, P2 p2) : T(p1, p2), m_count()
{
} template<typename P1, typename P2, typename P3>
RefCountedObject(P1 p1, P2 p2, P3 p3) : T(p1, p2, p3), m_count()
{
} template<typename P1, typename P2, typename P3, typename P4>
RefCountedObject(P1 p1, P2 p2, P3 p3, P4 p4)
: T(p1, p2, p3, p4), m_count()
{
} 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), m_count()
{
} virtual int AddRef()
{
return AtomicOps::Increment(&m_count);
} virtual int Release()
{
int count = AtomicOps::Decrement(&m_count);
if (!count)
{
delete this;
}
return count;
} protected:
virtual ~RefCountedObject() {
} int m_count;
};
使用方法如下:
class Test:public RefCountedBase
{
public:
void test(){}
}; //AddRef()及Release()方法由模板实现,无需再实现
RefCountedPtr<Test> test=new RefCountedObject<Test>()
test->test();
谷歌的C++智能指针实现的更多相关文章
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
- C++11 shared_ptr 智能指针 的使用,避免内存泄露
多线程程序经常会遇到在某个线程A创建了一个对象,这个对象需要在线程B使用, 在没有shared_ptr时,因为线程A,B结束时间不确定,即在A或B线程先释放这个对象都有可能造成另一个线程崩溃, 所以为 ...
- C++智能指针
引用计数技术及智能指针的简单实现 基础对象类 class Point { public: Point(int xVal = 0, int yVal = 0) : x(xVal), y(yVal) { ...
- EC笔记:第三部分:17、使用独立的语句将newed对象放入智能指针
一般的智能指针都是通过一个普通指针来初始化,所以很容易写出以下的代码: #include <iostream> using namespace std; int func1(){ //返回 ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...
- C++ 引用计数技术及智能指针的简单实现
一直以来都对智能指针一知半解,看C++Primer中也讲的不够清晰明白(大概是我功力不够吧).最近花了点时间认真看了智能指针,特地来写这篇文章. 1.智能指针是什么 简单来说,智能指针是一个类,它对普 ...
- C++11智能指针读书笔记;
智能指针是一个类对象,而非一个指针对象. 原始指针:通过new建立的*指针 智能指针:通过智能指针关键字(unique_ptr, shared_ptr ,weak_ptr)建立的指针 它的一种通用实现 ...
随机推荐
- C语言工具的简单辨析
- 蔡勒(Zeller)公式
蔡勒(Zeller)公式,是一个计算星期的公式,随便给一个日期,就能用这个公式推算出是星期几. W =[ [c/4] - 2c + y + [y/4] + [13 * (m+1) / 5] + d - ...
- 内容提供者(Content Provider)——跨程序共享数据
内容提供者 Content Provider 应用的数据库是不允许其他应用访问的 内容提供者的作用就是让别的应用访问到你的数据库 自定义内容提供者,继承ContentProvider类,重写增删改查方 ...
- 使用js实现html锚功能,可以任意定位锚的位置,比锚更加灵活
今天记录一个js功能,发现该功能很实用,拿出来跟大家分享. 这里我使用js的window.scrollTo()函数,该函数的用法给大家重复一下,语法为scrollTo(x,y),这里x表示在窗口文档显 ...
- 我是怎么拿到百度2016校招Offer的——IT技术类校招成功面经
笔者是软件工程大四学生,属于保研失败群体,不想考研,所以选择了工作.从9月下保研失败到10月中成功拿到百度Offer,这其中的大起大落真是外人所无法体会的.这一段时间的面试经验,可以和大家分享一下.笔 ...
- easyui 很好很强大
easyui 很好很强大 http://api.btboys.com/easyui/ 中文API教程 分页,拖动等效果很漂亮...
- Makefile 入门与基本语法 分类: C/C++ ubuntu 2015-05-18 11:16 466人阅读 评论(0) 收藏
在我看来,学会写简单的Makefile,阅读较复杂的makefile,是每一个Linux程序员都必须拥有的基本素质.Makefile可以自动识别哪些源文件被更改过,需要重新编译,那些不需要.从而节省大 ...
- mysql其他函数
mysql,,); +---------------+ ,,) | +---------------+ | +---------------+ row in set (0.22 sec) mysql) ...
- 字符串匹配算法-BM
在用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广泛的一种算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用Boyer-Moore算法.比我们学习的KMP算 ...
- 深度剖析:CDN内容分发网络技术原理--转载
1.前言 Internet的高速发展,给人们的工作和生活带来了极大的便利,对Internet的服务品质和访问速度要求越来越高,虽然带宽不断增加,用户数量也在不断增加,受Web服务器的负荷和传输距离等因 ...