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 ...
随机推荐
- C语言_了解下结构体指针
在C语言中几乎可以创建指向任何类型的指针,包括用户自定义的类型.当然也可以指向结构体,先看一个小案例: #include <stdio.h> #include <string.h&g ...
- NTP 时间同步协议
http://www.faqs.org/rfcs/rfc1305.html port:123
- UVA - 10285 Longest Run on a Snowboard (线性DP)
思路:d[x][y]表示以(x, y)作为起点能得到的最长递减序列,转移方程d[x][y] = max(d[px][py] + 1),此处(px, py)是它的相邻位置并且该位置的值小于(x, y)处 ...
- gm8180:arm linux启动加载模块、运行程序
1. init #!/bin/busybox ash#load modules mao 2013-02-16 14:12:48 echo "************************m ...
- WIN7下PS/2等键盘失灵无法使用的解决办法
WIN7下PS/2等键盘失灵无法使用的解决办法 装了win7,无意中一天开机,发现键盘不能用了.开始以为键盘坏了,重启看机,一看能进bios,各键正常.然后再重启,进系统,看设备管理器,发现键盘为黄色 ...
- Css中路径data:image/png;base64的用法详解
今天查看一些网站的css中发现了 background-image:url(data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAEAAAAkCAYAAAB ...
- jQuery提示parsererror错误解决办法
jquery来处理ajax,用到了json.但是很诧异,jquery的ajax回调时一直调用了error函数(一直提示parsererror异常),success函数一次没执行过 $.ajax({ t ...
- [Err] 1172 - Result consisted of more than one row
1 错误描述 [Err] 1172 - Result consisted of more than one row Procedure execution failed 1172 - Result c ...
- Java Web项目缺少jsp、servlet jar包
1.错误描述 Caused by:java.lang.ClassNotFoundException:javax.servlet.jsp.PageContent 2.错误原因 缺少有关的js ...
- web开发性能优化---UI界面篇
1.尽量采用div+css布局 DIV+CSS相比较与表格布局的优势: a.代码精简 使用DIV+CSS布局,页面代码精简,这一点对XHTML有所了解的都知道.代码精简所带来的直接好处有两点:一是提高 ...