#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. java swing 制作一个登陆界面,亲测有效

    一.介绍 Swing 是一个为Java设计的GUI工具包. Swing是JAVA基础类的一部分. Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表. Swing提供许多比AWT ...

  2. jquery查找筛选器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Delphi 使用MD5 比对文件

    使用MD5的方法比对CXimage里图片是否改变: Delphi7实现方法: uses IdHashMessageDigest function TForm1.GetImageMD5(cxImage: ...

  4. python oracle 查询返回字典

    from: https://sourceforge.net/p/cx-oracle/mailman/message/27145597/ I'd do it with a "row facto ...

  5. ACM__最小生成树之prime

    今天做了一道题,根本没想到最小生成树,稀里糊涂的浪费了很多时间,复习一下 转载自https://www.cnblogs.com/zhangming-blog/p/5414514.html Prim算法 ...

  6. C# 2018.9.17

    C#的优点:1,不会有运行时崩溃,解决了C++的痛点一,难预防,难查错2,使用文件不需要包含进来,只需要using namespace即可,解决了C++的痛点二,包含复杂,路径复杂,编译复杂3,编译速 ...

  7. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter

    在上一篇文章中Java IO流学习总结三:缓冲流-BufferedInputStream.BufferedOutputStream介绍了缓冲流中的字节流,而这一篇着重介绍缓冲流中字符流Buffered ...

  8. linux 3.10 的中断收包笔记

    来看下NAPI和非NAPI的区别: (1) 支持NAPI的网卡驱动必须提供轮询方法poll(). (2) 非NAPI的内核接口为netif_rx(),NAPI的内核接口为napi_schedule() ...

  9. kvm配置USB直通

    参照:https://www.linuxidc.com/Linux/2014-12/110919.htm WebVirMgr界面是没有直接的途径了,只能靠修改xml文件,在<device> ...

  10. 6.面向对象 -类.md

    目录 1. static: 2. 类在内存中,每一个类在创建在栈内存中,当创建一个对象的时候,将非类变量再堆内存中创建,而类变量是不会因为创建对象而在堆中重新创建 3. 对象.引用和指针: 4. 类名 ...