智能指针的实现代码来源博客:《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智能指针简单实现与应用的更多相关文章

  1. C++智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  2. 【转】C++智能指针简单剖析

    原文链接:http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看 <C++ Primer Plus>第六版,这的确是本好书 ...

  3. 【C++】智能指针简单剖析

    转自 http://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中 ...

  4. [转]C++智能指针简单剖析

    C++智能指针简单剖析  https://www.cnblogs.com/lanxuezaipiao/p/4132096.html 导读 最近在补看<C++ Primer Plus>第六版 ...

  5. C/C++ 智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

  6. C++智能指针及其简单实现

    本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...

  7. STL 智能指针

    转自: https://blog.csdn.net/k346k346/article/details/81478223 STL一共给我们提供了四种智能指针:auto_ptr.unique_ptr.sh ...

  8. C++之智能指针

    导读 一直对智能指针有一种神秘的赶脚,虽然平时没怎么用上智能指针,也就看过STL中的其中一种智能指针auto_ptr,但是一直好奇智能指针的设计因此,今天看了一下<C++ Primer Plus ...

  9. C++ 智能指针 auto_ptr 和 shared_ptr

    首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...

随机推荐

  1. 高可用之KeepAlived(一):基本概念和配置文件分析

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  2. java3 - 流程控制

    一.Java 有三种主要的循环结构: 需求:分别使用三种循环将 1 到 100 的整数输出到控制台. 1.for 循环 for(初始化语句; 布尔表达式语句; 更新语句) { //循环体内容 } 示列 ...

  3. C语言_来了解一下GCC编译器编译C可执行脚本的过程

    GCC简介    Linux系统下的gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的代表作品之一.gcc是可以在多种硬体平台上编译出可执行程序的超级编译 ...

  4. nyoj222 整数中的1 数位DP

    从a枚举到b是一定会超时的.此题应该考虑数位dp,也可以理解为递推,假设给定数n,就能在O(32)复杂度算出所有小于等于n的数中1出现的次数,那么给定区间[a, b],solve(b) - solve ...

  5. 项目实战14—ELK 企业内部日志分析系统

    一.els.elk 的介绍 1.els,elk els:ElasticSearch,Logstash,Kibana,Beats elk:ElasticSearch,Logstash,Kibana ① ...

  6. linux rsync实时同步

    rsync同步 同步与复制的差异:复制:完全拷贝源到目标同步:增量拷贝,只传输变化过的数据 同步操作:remote sync 远程同步支持本地复制,或与其他ssh,rsync主机同步.官方网站:htt ...

  7. 【Unity3D】Unity3D 摄像机带透明截图

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/CropCamera.html ↓↓↓下面的废话可以不看↓↓↓ 最近处理了一批我的游戏的图标,步骤特别繁琐, 需要先 ...

  8. Object Detection︱RCNN、faster-RCNN框架的浅读与延伸内容笔记

    一.RCNN,fast-RCNN.faster-RCNN进化史 本节由CDA深度学习课堂,唐宇迪老师教课,非常感谢唐老师课程中的论文解读,很有帮助. . 1.Selective search 如何寻找 ...

  9. R语言︱SNA-社会关系网络—igraph包(中心度、中心势)(二)

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- SNA社会关系网络分析中,关键的就是通过一些指 ...

  10. nginx重写rewrite的[emerg] unknown directive

    今天写nginx的重写规则.怎么写总是报这个错误.