#include <memory>
#include <iostream>
using namespace std; template<typename T>
class smart{
private:
T* _ptr;
int* _count; //reference counting
public:
//构造函数
smart(T* ptr = nullptr):_ptr(ptr){
if (_ptr){
_count = new int(1);
}
else{
_count = new int(0);
}
} //拷贝构造
smart(const smart& ptr){
if (this != &ptr){
this->_ptr = ptr._ptr;
this->_count = ptr._count; (*this->_count)++;
}
} //重载operator=
smart operator=(const smart& ptr){
if (this->_ptr == ptr._ptr){
return *this;
}
if (this->_ptr){
(*this->_count)--;
if (this->_count == 0){
delete this->_ptr;
delete this->_count;
}
}
this->_ptr = ptr._ptr;
this->_count = ptr._count;
(*this->_count)++;
return *this;
} //operator*重载
T& operator*(){
if (this->_ptr){
return *(this->_ptr);
}
} //operator->重载
T& operator->(){
if (this->_ptr){
return this->_ptr;
}
}
//析构函数
~smart(){
(*this->_count)--;
if (*this->_count == 0){
delete this->_ptr;
delete this->_count;
}
}
//return reference counting
int use_count(){
return *this->_count;
}
}; int main(){
smart<int> sm(new int(10));
cout << "operator*():" << *sm << endl;
cout << "reference counting:(sm)" << sm.use_count() << endl;
smart<int> sm2(sm);
cout <<"copy ctor reference counting:(sm)"<< sm.use_count() << endl;
smart<int> sm3;
sm3 = sm;
cout <<"copy operator= reference counting:(sm)"<< sm.use_count() << endl;
cout << &sm << endl;
return 0;
}

  

C++ 智能指针shared_ptr的实现的更多相关文章

  1. c/c++ 智能指针 shared_ptr 和 new结合使用

    智能指针 shared_ptr 和 new结合使用 用make_shared函数初始化shared_ptr是最推荐的,但有的时候还是需要用new关键字来初始化shared_ptr. 一,先来个表格,唠 ...

  2. c/c++ 智能指针 shared_ptr 使用

    智能指针 shared_ptr 使用 上一篇智能指针是啥玩意,介绍了什么是智能指针. 这一篇简单说说如何使用智能指针. 一,智能指针分3类:今天只唠唠shared_ptr shared_ptr uni ...

  3. C++智能指针shared_ptr

    shared_ptr 这里有一个你在标准库中找不到的—引用数智能指针.大部分人都应当有过使用智能指针的经历,并且已经有很多关于引用数的文章.最重要的一个细节是引用数是如何被执行的—插入,意思是说你将引 ...

  4. STL源码剖析-智能指针shared_ptr源码

    目录一. 引言二. 代码实现 2.1 模拟实现shared_ptr2.2 测试用例三. 潜在问题分析 你可能还需要了解模拟实现C++标准库中的auto_ptr一. 引言与auto_ptr大同小异,sh ...

  5. 智能指针shared_ptr的用法

    为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...

  6. 智能指针 shared_ptr 解析

    近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM   @HQU s ...

  7. 智能指针shared_ptr

    // 智能指针会自动释放所指向的对象. // shared_ptr的应用场景是:程序需要在多个对象间共享数据 /* 先从应用场景入手吧,说矿工A发现了一个金矿. * 然后矿工A喊来了矿工B,一起开采, ...

  8. 智能指针shared_ptr新特性shared_from_this及weak_ptr

    enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...

  9. C++ 智能指针 shared_ptr

    今天晚上去旁听了C++高级编程的课,其中提到智能指针.第一反映还以为是auto_ptr呢,一听才知道是share_ptr这个.哦,原来是C++11特性.大致的原因是auto_ptr有一点缺陷,而sha ...

  10. 标准库中的智能指针shared_ptr

    智能指针的出现是为了能够更加方便的解决动态内存的管理问题.注:曾经记得有本书上说可以通过vector来实现动态分配的内存的自动管理,但是经过试验,在gcc4.8.5下是不行的.这个是容易理解的,vec ...

随机推荐

  1. Linux系统基础5周入门精讲(服务器介绍)

    使用工具:(1)源码管理(git工具):(2)自动部署:(3)web服务器 linux基础 服务器介绍 运维的职责:运行和维护服务器 1数据不能丢----大片不能没 2保证网站7*24小时允许---( ...

  2. SpringBoot配置发送邮件

    一.导入jar包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  3. redis 学习笔记3(哨兵模式下分布式锁的实现以及全局唯一id的生成)

    redis实现分布式锁和全局唯一id应该是较为常见的应用. 实现基于redis的setNX,以及incr命令.还是比较简单的! 搭建环境以及配置好sping整合,做了下测试,有兴趣的载下来看看,自己做 ...

  4. ubuntu 安装 oracle sql developer

    安装java-jdk 这一部分教程很多,可按照网上教程进行安装 安装sqldeveloper 下载 sqldeveloper Linux RPM http://www.oracle.com/techn ...

  5. vue.js 作一个用户表添加页面----初级

    使用vue.js 制作一个用户表添加页面,实际上是把原来需要使用js写的部分,改写成vue.js的格式 首先,想象一下,先做思考,我们要添加用户表,设涉及到哪些数据,一个是用户id,一个是用户名,一个 ...

  6. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  7. DELPHI 对象的本质 VMT

    http://www.cnblogs.com/little-mat/articles/2206627.html TObject是所有对象的基本类,DELPHI中的任何对象都是一个指针,这个指针指明该对 ...

  8. Ajax 学习 第二篇

    XMLHttpRequest发送请求 open(method,url,async) 解释 method:do/post,不区分大小写 URL:相对地址 文档地址 async:默认为TRUE 即异步 F ...

  9. 常用dos命令和windows系统快捷键

    一.dos命令[重难点]1.OS——操作系统2.如何进入dos窗口?——P111 开始——程序——运行——输入cmd3.常用的dos操作命令 3.1 返回上一级 cd.. 3.2 返回到根目录 cd\ ...

  10. 学习BOS物流项目第九天

    1 教学计划 1.业务受理需求分析 a. 业务通知单 b.工单 c.工作单 2.创建业务受理环节的数据表 a.业务通知单 b.工单 c.工作单 3.实现业务受理自动分单 a.在CRM服务端扩展方法根据 ...