Part2中的智能指针:

#ifndef _SMARTPTR_H_
#define _SMARTPTR_H_#include <cassert>#ifndef NULL
#define NULL    0
#endif// A template class that provides smart pointer functionality.
/*The principle is that each instance of this class holds a pointer to a structurethat contains a reference counter of the object and the pointer to the object itself.The reference is incremented each time the smart pointer is copied and decremented each time the smart pointer is destroyed.When no one reference the pointer anymore, the pointed object is destroyed.
*/
template <class T>
class CSmartPtr
{
public://! Default constructor. Initializes the pointer to NULLCSmartPtr(){m_pPtrInfo = NULL;}// Contructor with a pointer to track. pPointee is the pointer to keep // track of. Note that the smart pointer will keep ownership of this // pointer and will delete it itself, so don't delete it  yourself.CSmartPtr(T* pPointee){m_pPtrInfo = new TSharedData;m_pPtrInfo->m_pPointee = pPointee;m_pPtrInfo->m_iRefCount = 1;}// Copy constructor. The argument is a smart pointer to make a copy of. // The smart pointer will increment the reference counter of the pointed object.CSmartPtr(const CSmartPtr& other){m_pPtrInfo = other.m_pPtrInfo;if (m_pPtrInfo)m_pPtrInfo->m_iRefCount++;}// Assignment operator. The argument is a smart pointer to make a copy of. // The smart pointer will increment the reference counter of the pointed // object. If the smart pointer was already tracking a variable, the reference // counter for this variable will be decremented (and the pointer destroyed // if it becomes 0).CSmartPtr& operator=(const CSmartPtr& other){if (this != &other){if (m_pPtrInfo){m_pPtrInfo->m_iRefCount--;if (m_pPtrInfo->m_iRefCount == NULL){delete m_pPtrInfo->m_pPointee;delete m_pPtrInfo;}}m_pPtrInfo = other.m_pPtrInfo;if (m_pPtrInfo)m_pPtrInfo->m_iRefCount++;}return *this;}// Destructor. It decrements the shared reference counter. // If it becomes 0, the pointed variable is destroyed.~CSmartPtr()  { if (m_pPtrInfo){m_pPtrInfo->m_iRefCount--;if (m_pPtrInfo->m_iRefCount == 0){delete m_pPtrInfo->m_pPointee;delete m_pPtrInfo;}}}// Overloading of the * operator to access the contents of the pointed variable.T& operator* () const  { assert(m_pPtrInfo != NULL);return *(m_pPtrInfo->m_pPointee); }// Overloading of the -> operator that returns the pointer to the variable.T* operator->() const  { assert(m_pPtrInfo != NULL);return m_pPtrInfo->m_pPointee; }// Check to see if the pointer to the variable is NULL.bool isNull() const  { if (m_pPtrInfo && m_pPtrInfo->m_pPointee)return false;return true;}private:// Structure shared across smart pointers.struct TSharedData{T* m_pPointee;int m_iRefCount;};TSharedData* m_pPtrInfo;
};#endif  // _SMARTPTR_H_

简单使用:

        typedef CSmartPtr<CImage> TImagePtr;TImagePtr imgPtr(new CImage(strFileName));

Part2:CSmartPtr相关推荐

  1. Linux平台 Oracle 18c RAC安装Part2:GI配置

    三.GI(Grid Infrastructure)安装 3.1 解压GI的安装包 3.2 安装配置Xmanager软件 3.3 共享存储LUN的赋权 3.4 使用Xmanager图形化界面配置GI 3 ...

  2. LYNC2013部署系列PART2:后端部署

    LYNC2013部署系列PART2:后端部署 前言:本篇文章介绍lync后端服务器的部署,先部署好2台后端数据库服务器,分别为lync2013be.contoso.com和lync2013db1.co ...

  3. 强化学习:Policy-based方法Part2

    在Part1部分,我们学习了什么是策略梯度,以及该算法的优势与劣势,在Part2部分,我们将学习到如何通过策略搜索实现策略函数的迭代优化. 目前,我们已经知道了基于策略的方法具有求解稳定.搜索效果好. ...

  4. CS231n 学习笔记(2)——神经网络 part2 :Softmax classifier

    *此系列为斯坦福李飞飞团队的系列公开课"cs231n convolutional neural network for visual recognition "的学习笔记.本文主要 ...

  5. 【未来可能用到】关于模型的100个问答-part2

    关于模型的100个问答-part2 一 距离过年还有8天,没错的,我跟你一样还没有休假.深圳这段时间是冷到刺骨了,就是冷到我今天才来更新,不要烦我每次都要说这段话,毕竟是生活中无处表达,只能在这抒发了 ...

  6. (转)[EntLib]微软企业库5.0 学习之路——第十步、使用Unity解耦你的系统—PART2——了解Unity的使用方法(1)...

    原文地址:http://www.cnblogs.com/kyo-yo/archive/2010/11/01/Learning-EntLib-Tenth-Decoupling-Your-System-U ...

  7. 黑客与画家 part1 版权声明 part2 O'Reilly Media,Ina.介绍

    part1 版权声明 page 11 版权声明 英文原版O'Reilly Media,Ina.出版社2004. 简体中文版由人民邮电出版社出版,2011.英文原版的翻译得到O'Reilly Media ...

  8. Silverlight中摄像头的运用—part2

    Silverlight 4 中摄像头的运用-part1 将跟踪颜色视作输入  好了,我们能够跟踪到这个颜色了,那这么做的意义是什么呢?实际上,我们可以根据它的位置来移动东西.接下来的例子中,创建的一个 ...

  9. android source镜像源_【转载】Celadon快速上路指南Part2:编译Celadon镜像

    Celadon快速上路指南Part2:编译Celadon镜像 From: 孙晓璐 AndroidIA Celadon 9/20 一目了然 | Celadon 新手上路快速通道隆重揭晓 | 打开Cela ...

最新文章

  1. 独家 | 如何在BigQueryML中使用K-均值聚类来更好地理解和描述数据(附代码)
  2. 智能化家庭弱电布线标准规范
  3. 知乎云敲钟、比特币可购买特斯拉、用户隐私收集新规五一生效、 软件疯长等|Decode the Week...
  4. 关于printf输出 格式化规定符 的
  5. SpringMVC整合Shiro
  6. c语言linux TCP长连接 socket收发范例 断开自动重连
  7. PAT乙类1013 数素数 (20 分)
  8. 利用Python中的GDAL和OGR模块实现shapefile对栅格DEM数据的裁剪
  9. Qt网络编程-简易版UDP单播通信入门Demo(3)
  10. win10下实现bat转exe
  11. n1进入recovery模式_OPPO N1如何进入recovery模式
  12. 人人网惨遭全网下架,但是我并不同情他!
  13. 杭州电子科技大学计算机非全日制,杭州电子科技大学非全日制研究生考试难吗?...
  14. 如何在阿里云服务器部署程序并用域名直接访问
  15. table添加一行且可编辑 vue_Vue使用AntDesign 表格可添加 可编辑行 可选择
  16. 【无标题】程序员的一大步
  17. 能自动翻译的软件-最精准的翻译软件
  18. PHP两种调用接口方式
  19. Vue 的响应式原理中 Object.defineProperty 有什么缺陷?为什么在 Vue3.0 采用了 Proxy,抛弃了 Object.defineProperty?...
  20. 【夏目鬼鬼分享】springboot搭建阿里Druid数据源监控

热门文章

  1. 如何恢复 Linux 上删除的文件:ext2
  2. win business_使用Business iQ进行实时业务监控
  3. win10平板续航测试软件,Win10平板最低配置:续航最低8小时
  4. 哈佛经典:领导者应该做什么?(领导与管理的不同)
  5. mysql insert 1366_mysql insert中文乱码无法插入ERROR 1366 (HY000): Incorrect string value
  6. “笨办法”学Python3,Zed A. Shaw,习题15
  7. Mac和Ubuntu系统下.bash_profile和.bashrc文件
  8. 模糊数学——分解定理
  9. 微信直播聊天室单房间1500万在线的消息架构演进之路
  10. 怎样理解人生观、价值观、世界观?