谷歌的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)建立的指针 它的一种通用实现 ...
随机推荐
- Ubuntu---2
1.Ubuntu学习笔记之:安装中文语言包 http://askubuntu.com/questions/59356/how-do-i-get-chinese-input-to-work
- Hadoop之hive安装过程以及运行常见问题
Hive简介 1.数据仓库工具 2.支持一种与Sql类似的语言HiveQL 3.可以看成是从Sql到MapReduce的映射器 4.提供shall.Jdbc/odbc.Thrift.Web等接口 Hi ...
- 《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的分布
在讨论连续型随机变量函数的分布时,我们从一般的情况中(讨论正态分布的文章中提及),能够得到简化版模型. 回忆利用分布函数和概率密度的关系求解随机变量函数分布的过程,有Y=g(x),如果g(x)是严格单 ...
- ARC __bridge modifiers demystified
http://stackoverflow.com/questions/14207960/arc-bridge-modifiers-demystified Because I learned what ...
- 【转】shell 教程——06 Shell变量:Shell变量的定义、删除变量、只读变量、变量类型
Shell支持自定义变量. 定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编 ...
- Oracle的OFA架构
最优灵活体系结构(Optimal Flexible Architecture,简称OFA) OFA其实就是一种Oracle的一种规范,其意义就是用一种统一的给文件和文件夹的规则,和文件存放目录的规则做 ...
- struts2 表单处理
在这篇教程里我们将探究如何处理表单提交.本文例子介绍: javabean存储表单数据 在action中重写validate方法进行简单的校验 创建一个struts2表单并和javabean匹配 jav ...
- JDBC——事物管理
案例:银行转账问题,数据库如下 相关API setAutoCommit(boolean autoCommit) 将此连接的自动提交模式设置为给定状态.设置事务是否自动提交如果设置为false,表示手 ...
- [MySQL] MySQL的自己主动化安装部署
有过MySQL运维的人应该都清楚,线上的MySQL一般都採用源代码编译,由于这样才干够依据企业的各自须要选择要编译的功能,尽管MySQL的源代码编译挺简单的,可是试想一下,假设你有几百台server同 ...
- SQL Server与Oracle对比学习:权限管理(二) 一些有趣的比喻
http://blog.csdn.net/weiwenhp/article/details/8094739 目录(?)[-] SQL Server权限管理 login 与user的区别 角色role ...