C++ RCSP智能指针简单实现与应用
智能指针的实现代码来源博客:《http://blog.csdn.net/to_be_better/article/details/53570910》
修改:添加 get()函数,用以获得原始指针(raw pointer)。
其余思路来源《Effective C++》
智能指针的实现代码如下:
template <typename T>
class SmartPtr;
template <typename T>
class Ptr
{
friend class SmartPtr<T>; T *m_ptr;
size_t m_count; Ptr(T *p = NULL) : m_ptr(p), m_count() {}
~Ptr()
{
delete m_ptr;
}
};
template <typename T>
class SmartPtr
{
public:
SmartPtr(T *p = NULL) : m_p(new Ptr<T>(p)) {}
SmartPtr(const SmartPtr &sp) : m_p(sp.m_p)
{
++m_p->m_count;
} SmartPtr &operator=(const SmartPtr &sp)
{
++sp.m_p->m_count;
if (--m_p->m_count == )
{
delete m_p;
}
m_p = sp.m_p; return *this;
} T *operator->() { return m_p->m_ptr; }
const T *operator->() const { return m_p->m_ptr; } T operator*() { return *m_p->m_ptr; }
T *get() /*get raw pointer*/
{
return m_p->m_ptr;
}
~SmartPtr()
{
if (--m_p->m_count == )
delete m_p;
} private:
Ptr<T> *m_p;
};
引用计数型智能指针(reference-counting smart pointer, RCSP)可实现持续追踪共有多少对象指向某笔资源,并在无人指向它时自动删除该资源。
在c++中资源管理中为防止意外退出而导致资源泄漏。
这种reference counting 可以允许copying行为,如需抑制copying,以private 方式继承Uncopyable类即可。
Uncopyable类:
class Uncopyable
{
protected:
Uncopyable() {}
~Uncopyable() {} private:
Uncopyable(const Uncopyable &);
Uncopyable &operator=(const Uncopyable &);
};
一个应用例子:
目的是创建一个类的智能指针,用以描述文件的一些属性的类,在后续代码中使用这个指针来赋予或读取这些属性。当然,使用智能指针为了防止资源泄漏,符合本文初衷。
由成员函数:createFileAttrs() 产生动态创建一个静态的智能指针,由这个指针去给类中的成员变量分配资源,并返回这个指针,即可实现功能。
测试类:
class FileAttr
{
public:
~FileAttr();
static SmartPtr<FileAttr> createFileAttrs();
char *md5;
private:
FileAttr();
};
FileAttr::FileAttr()
{}
FileAttr::~FileAttr()
{
cout << "destructor" << endl;
delete[] md5;
}
SmartPtr<FileAttr> FileAttr::createFileAttrs()
{
static SmartPtr<FileAttr> fileAttr(new FileAttr());
fileAttr->md5 = new char[];
return fileAttr;
}
应用方法:
int main()
{
SmartPtr<FileAttr> fa = FileAttr::createFileAttrs(); // 使用智能指针
/* FileAttr *fa = FileAttr::createFileAttrs().get(); // 或者使用原始指针 */
{
memcpy(fa->md5, "md51", );
}
{
memcpy(fa->md5 + , "md52", );
}
cout << fa->md5<<endl;
return ;
}
打印输出:
md51md52
destructor
由于自定义类未重载operator=,所以直接使用智能指针比较合适,需要原始指针的话调用get()函数即可。
C++ RCSP智能指针简单实现与应用的更多相关文章
- C++智能指针简单剖析
导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...
- 【转】C++智能指针简单剖析
原文链接:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看 <C++ Primer Plus>第六版,这的确是本好书 ...
- 【C++】智能指针简单剖析
转自 http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中 ...
- [转]C++智能指针简单剖析
C++智能指针简单剖析 https://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版 ...
- C/C++ 智能指针简单剖析
导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...
- C++智能指针及其简单实现
本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...
- STL 智能指针
转自: https://blog.csdn.net/k346k346/article/details/81478223 STL一共给我们提供了四种智能指针:auto_ptr.unique_ptr.sh ...
- C++之智能指针
导读 一直对智能指针有一种神秘的赶脚,虽然平时没怎么用上智能指针,也就看过STL中的其中一种智能指针auto_ptr,但是一直好奇智能指针的设计因此,今天看了一下<C++ Primer Plus ...
- C++ 智能指针 auto_ptr 和 shared_ptr
首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...
随机推荐
- MySQL Server 5.0安装教程
相信很多朋友刚开始接触mysql数据库服务器,下面是mysql的安装教程,步骤明细也有详细的说明. 工具/原料 mysql MySQL安装的图解 1 打开下载的mysql安装文件mysql-5 ...
- SpringMvc Ant通配符的使用
@RequestMapping使用通配符来对地址进行映射 Ant 的3风格 – ? 匹配文件名中的一个字符 – * 匹配文件名中的任意字符 – ** ** 匹配多重路径 例如:RequestMapp ...
- 老男孩Python全栈开发(92天全)视频教程 自学笔记16
day16课程内容: 装饰器: def outer(): x=10 def inner(): print(x) return innerouter()() #inner 是局部变量,10闭包:如果在一 ...
- 试着把.net的GC讲清楚(3)
前两篇写的都是gc的一些概念和细节,这些东西对自己以后写代码有什么用,本篇我就准备将这些内容. root 第一篇文章中讲了GC在遍历存活对象的时候,都是从root开始的,root是一些对象的引用,例如 ...
- Qt Create or VS 2015 使用 Opencv330 相机静态库链接错误如何解决?
查看链接库,添加 vfw32.lib 即可.
- HDU - 2181 dfs [kuangbin带你飞]专题二
保存每个节点的下一个节点一直往下面走就行了,不能重复经过某个点,当经过的点达到20个而且当前节点的下一个节点是起点就打印答案. AC代码 #include<cstdio> #include ...
- 妙用ES6解构和扩展运算符让你的代码更优雅
http://www.cnblogs.com/chrischjh/p/4848934.html
- java Socket实现简单在线聊天(三)
在上一篇,利用线程使服务端实现了能够接收多客户端请求的功能,这里便需要客户端接收多客户端消息的同时还能把消息转发到每个连接的客户端,并且客户端要能在内容显示区域显示出来,从而实现简单的在线群聊. 在实 ...
- 用Dw CS6运行静态页面出问题
1.报错截图一 2.报错截图二
- ASP.NET Core轻松入门之Configure中IHostingEnvironment和IApplicationLifetime的使用
在StratUp.cs的Configure方法中,除了 常用的参数除了IApplicationBuilder和在我上一篇文章中提到的Iconfiguration点击打开链接 外 还有 IHostin ...