C++ 智能指针shared_ptr的实现
#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的实现的更多相关文章
- c/c++ 智能指针 shared_ptr 和 new结合使用
智能指针 shared_ptr 和 new结合使用 用make_shared函数初始化shared_ptr是最推荐的,但有的时候还是需要用new关键字来初始化shared_ptr. 一,先来个表格,唠 ...
- c/c++ 智能指针 shared_ptr 使用
智能指针 shared_ptr 使用 上一篇智能指针是啥玩意,介绍了什么是智能指针. 这一篇简单说说如何使用智能指针. 一,智能指针分3类:今天只唠唠shared_ptr shared_ptr uni ...
- C++智能指针shared_ptr
shared_ptr 这里有一个你在标准库中找不到的—引用数智能指针.大部分人都应当有过使用智能指针的经历,并且已经有很多关于引用数的文章.最重要的一个细节是引用数是如何被执行的—插入,意思是说你将引 ...
- STL源码剖析-智能指针shared_ptr源码
目录一. 引言二. 代码实现 2.1 模拟实现shared_ptr2.2 测试用例三. 潜在问题分析 你可能还需要了解模拟实现C++标准库中的auto_ptr一. 引言与auto_ptr大同小异,sh ...
- 智能指针shared_ptr的用法
为了解决C++内存泄漏的问题,C++11引入了智能指针(Smart Pointer). 智能指针的原理是,接受一个申请好的内存地址,构造一个保存在栈上的智能指针对象,当程序退出栈的作用域范围后,由于栈 ...
- 智能指针 shared_ptr 解析
近期正在进行<Effective C++>的第二遍阅读,书里面多个条款涉及到了shared_ptr智能指针,介绍的太分散,学习起来麻烦.写篇blog整理一下. LinJM @HQU s ...
- 智能指针shared_ptr
// 智能指针会自动释放所指向的对象. // shared_ptr的应用场景是:程序需要在多个对象间共享数据 /* 先从应用场景入手吧,说矿工A发现了一个金矿. * 然后矿工A喊来了矿工B,一起开采, ...
- 智能指针shared_ptr新特性shared_from_this及weak_ptr
enable_shared_from_this是一个模板类,定义于头文件<memory>,其原型为: template< class T > class enable_shar ...
- C++ 智能指针 shared_ptr
今天晚上去旁听了C++高级编程的课,其中提到智能指针.第一反映还以为是auto_ptr呢,一听才知道是share_ptr这个.哦,原来是C++11特性.大致的原因是auto_ptr有一点缺陷,而sha ...
- 标准库中的智能指针shared_ptr
智能指针的出现是为了能够更加方便的解决动态内存的管理问题.注:曾经记得有本书上说可以通过vector来实现动态分配的内存的自动管理,但是经过试验,在gcc4.8.5下是不行的.这个是容易理解的,vec ...
随机推荐
- iOS开发 2x 3x图
众所周知,iOS开发中的图片资源一般需要2倍图和3倍图,也就是2x,3x,但是最近思考了一个问题,为什么不能只提供3x的图片,2x的图片让系统从3x压缩就好了,于是上网搜索了下,得到了答案. 当我们在 ...
- Haskell语言开发工具
Stack How to Script with Stack Originate Guides - Haskell Tool Stack 配置 Intellij Idea IntelliJ plugi ...
- avalon2学习教程04显示隐藏处理
今天的主角是ms-visible,它的效果类拟于jQuery的toggle,如果它后面跟着的表达式为真值时则显示它所在的元素,为假值时则隐藏.不过显示不是 display:none这么简单,众所周知, ...
- 何谓domReady
我的博客已经写过好几篇如何实现domReady的文章,最近做培训,面向新手们,需要彻彻底底向他们说明这个东西,于是就有了这篇文章. 我们经常看人们用 document.getElementById(& ...
- 一张图知道HTML5布局(图)
- Struct2.0学习笔记1
为了更好的配合队友写项目 现在学习如下 1.目录 2. 3. Struct2-Action 配置环境 4. 改action 名字 不用重启服务器(从上面粘贴) 改成true 即开发模式 5.想看源码 ...
- 扩展C#与元编程
扩展C#与元编程 https://www.cnblogs.com/knat/p/4580393.html https://www.cnblogs.com/knat/p/4584023.html 扩展C ...
- swift 头尾式动画
1.0 头尾式动画 UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1.0) // 设置执行动画所需要的时间 ...
- web自动化测试中接口测试学习笔记
一.web基础 web是实现:客户端浏览器端<—————>服务端 交互的应用: web通常包含两部分:web客户端.web服务端:web客户端技术包含html.javascript.aj ...
- LinQ to sql 各种数据库查询方法
1.多条件查询: 并且 && 或者 || var list = con.car.Where(r => r.code == "c014" || r.oil == ...