智能指针的实现代码来源博客:《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(1) {}~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 == 0){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 == 0)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[20];return fileAttr;
}

应用方法:

int main()
{SmartPtr<FileAttr> fa = FileAttr::createFileAttrs(); // 使用智能指针/* FileAttr *fa = FileAttr::createFileAttrs().get(); // 或者使用原始指针 */{memcpy(fa->md5, "md51", 4);}   {memcpy(fa->md5 + 4, "md52", 4);}cout << fa->md5<<endl;return 0;
}

打印输出:

md51md52
destructor

由于自定义类未重载operator=,所以直接使用智能指针比较合适,需要原始指针的话调用get()函数即可。

转载于:https://www.cnblogs.com/bobojiang/p/8580015.html

C++ RCSP智能指针简单实现与应用相关推荐

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

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

  2. C++智能指针简单介绍

    STL一共给我们提供了四种智能指针:auto_ptr.unique_ptr.shared_ptr和weak_ptr(本文章暂不讨论). 模板auto_ptr是C++98提供的解决方案,C+11已将将其 ...

  3. c++智能指针简单使用

    一. 智能指针的基本作用 一句话带过:智能指针就是帮我们C++程序员管理动态分配的内存的,它会帮助我们自动释放new出来的内存,从而避免内存泄漏! 二. 一个内存泄露的例子 #include < ...

  4. c++中的smart pointer四个智能指针简单介绍

    C++里面的四个智能指针: auto_ptr, shared_ptr, weak_ptr, unique_ptr 其中后三个是c++11支持,并且第一个已经被11弃用. 为什么要使用智能指针: 智能指 ...

  5. [C++11]智能指针简单介绍

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

    原文:http://www.cnblogs.com/xiehongfeng100/p/4645555.html C++智能指针及其简单实现 本文将简要介绍智能指针shared_ptr和unique_p ...

  7. 【C++ 语言】智能指针 引入 ( 内存泄漏 | 智能指针简介 | 简单示例 )

    文章目录 I . 智能指针 引入 II . 智能指针 简介 III . 智能指针 简单示例 I . 智能指针 引入 1 . 示例前提 : 定义一个 Student 类 , 之后将该类对象作为智能指针指 ...

  8. 【C++】智能指针(一)入门

    1. 智能指针背后的设计思想 智能指针背后的思想是RAII,参见博客[C++]零散知识 我们先来看一个简单的例子: void remodel(std::string & str) {std:: ...

  9. C++ 智能指针简介

    1.智能指针的由来 C++ 中,动态内存的管理是通过一对运算符来完成的,new 用于申请内存空间,调用对象构造函数初始化对象并返回指向该对象的指针.delete接收一个动态对象的指针,调用对象的析构函 ...

最新文章

  1. [精选]MySQL的各种锁(表锁,行锁,悲观锁,乐观锁,间隙锁,死锁)
  2. Filemanager 的使用
  3. 【DataGuard】ORA-16014 and ORA-00312 Messages in Alert.log of Physical Standby
  4. mysql 测试快生产慢_生产上MySQL慢查询优化实战,SQL优化实战
  5. asp和php数据库怎么区分,asp与php的数据库有哪些区别
  6. 微软云计算介绍与实践(实践之三十五)
  7. 机器学习之监督学习(二)——神经网络
  8. 螃蟹保存方法保存时间_中秋吃不完的螃蟹如何保存?蟹农:生螃蟹冰箱冷藏,熟螃蟹别隔夜...
  9. 高中信息技术——进制与编码刷题点整理
  10. System.Timers.Timer 与 System.Threading.Timer 小间隔
  11. 算法笔记_面试题_10.所有可能的满二叉树
  12. 远程预付费电能管理系统在工业园的应用,主要功能有哪些?
  13. ToStringBuilder学习总结
  14. SkipList算法原理分析
  15. WEB云安全技术应用篇
  16. [研究生]你该如何“精读”一篇文章?文献管理与文献笔记:以VCNet为例
  17. STEP2——《数据分析:企业的贤内助》重点摘要笔记(四)——数据清洗
  18. 搜狗双拼如何打单韵母字
  19. linux iptables mac,mac下的iptables---pfctl
  20. 计算机内存的安装方法,内存条怎么装?内存条安装与拆卸方法

热门文章

  1. 原生javascript实现放大镜效果
  2. Windows内存管理和linux内存管理
  3. 使用dd命令复制ASM磁盘的spfile
  4. OI基础系列之最大子数组问题
  5. 条款22: 尽量用“传引用”而不用“传值”
  6. 线性时间选择问题——分治
  7. 对MySQL进行逻辑卷备份与恢复
  8. Oracle嵌套表实例说明
  9. Hibernate和iBATIS 优缺点比较
  10. 排序算法java版,速度排行:冒泡排序、简单选择排序、直接插入排序、折半插入排序、希尔排序、堆排序、归并排序、快速排序...