谷歌的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)建立的指针 它的一种通用实现 ...
随机推荐
- 【转】Flask快速入门
迫不及待要开始了吗?本页提供了一个很好的 Flask 介绍,并假定你已经安装好了 Flask.如果没有,请跳转到 安装 章节. 一个最小的应用 一个最小的 Flask 应用看起来会是这样: from ...
- Code Forces Gym 100971D Laying Cables(单调栈)
D - Laying Cables Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- Java Web学习笔记-Servlet不是线程安全的
由于Servlet只会有一个实例,多个用户同时请求同一个Servlet时,Tomcat会派生出多条线程执行Servlet的代码,因此Servlet有线程不安全的隐患.如果设计不当,系统就会出现问题. ...
- clas
- Yii 将对象转化成数组
将从数据库查找的对象,转换成数组,并且以设定属性键名称,用到ArrayHelper::toArray $posts = Post::find()->limit(10)->all(); $d ...
- 3proxy代理软件文档说明
官方英文原版说明:http://www.3proxy.ru/howtoe.asp 配置文件的简要说明:如果你的英文理解力好,可以试着研究一下他的手册. 以实例说明吧 nscache 65536域名解析 ...
- Xilinx ISE14.7 安装教程(转)
文章来源http://blog.chinaaet.com/crazybird/p/39693 作者:crazybird **************************************** ...
- XMind十大最有用的功能
XMind十大最有用的功能 XMind是一款顶级商业品质的思维导图软件和头脑风暴软件,在企业和教育领域都有很广泛的应用,XMind功能全面,易上手,在此小编给大家整理出了XMind十大最有用的功能以供 ...
- iOS swift使用xib绘制UIView
目标:用xib绘制一个UIView,在某个ViewController中调用. 三个文件:ViewController.Swift DemoView.swift DemoView.xib ...
- myeclipse 编码问题
在使用eclipse+MyEclipse开发中,许多文件编码默认是ISO-8859-1,不支持中文(如常用的JSP),这样我们每次建文件都要手动改编码,其实我们可以在设置文件默认编码,今后再创建时就不 ...