模拟实现shared_ptr
来源:互联网 发布:批视频有哪些软件 编辑:程序博客网 时间:2024/05/22 03:51
模拟实现shared_ptr
template <class T>class sharedptr{public:sharedptr(T*ptr = NULL):_ptr(ptr), _pcount(NULL){if (_ptr != NULL){_pcount = new int(1);}}sharedptr(const sharedptr<T>&sp):_ptr(sp._ptr), _pcount(sp._pcount){*_pcount += 1;}sharedptr<T>& operator=(const sharedptr<T>&sp){if (_ptr != sp._ptr){//if (_pcount&&--*_pcount == 0)//当前对象独自管理空间//{//delete _ptr;//_ptr = NULL;//delete _pcount;//_pcount = NULL;//}//当前对象没有管理空间或者赋值者为nullrelese();_ptr = sp._ptr;_pcount = sp._pcount;if (_pcount != NULL){(*_pcount)++;}}return *this;}~sharedptr(){//if (_pcount != NULL&& --*_pcount == 0)//说明有对象并且只有一个对象管理空间//{//delete _ptr;//_ptr = NULL;//delete _pcount;//_pcount = NULL;//}relese();}T& operator*(){return *_ptr;}T operator->(){return _ptr;}private:void relese(){if (_pcount != NULL&& --*_pcount == 0)//说明有对象并且只有一个对象管理空间{delete _ptr;_ptr = NULL;delete _pcount;_pcount = NULL;}}private:T* _ptr;int* _pcount;};int main(){//sharedptr<int>sp1(new int);//sharedptr<int>sp2(sp1);sharedptr<int>sp3(new int);//sp3 = sp2;//当前对象独自管理一块空间//sp2 = sp3;//当前对象和sp1共享一块空间sharedptr<int>sp4;//sp4 = sp3;//当前对象没有管理空间sp3 = sp4;//空对象给有对象赋值system("pause");return 0;}
阅读全文
0 0
- 模拟实现shared_ptr
- 模拟实现shared_ptr
- 智能指针:模拟实现auto_ptr,scoped_ptr,shared_ptr
- 模拟实现auto_ptr、scoped_ptr、shared_ptr等智能指针
- 智能指针的模拟实现 auto_ptr scoped_ptr shared_ptr
- shared_ptr 实现
- shared_ptr 实现
- shared_ptr模拟代码
- 智能指针(模拟实现auto_ptr,shared_ptr,scooeptr 以及定制删除器c++ 实现)
- 模拟 C++11 的shared_ptr
- 智能指针的模拟实现shared_ptr 循环引用 定置删除器
- 【C++】智能指针的作用,模拟实现auto_ptr,scoped_ptr,shared_ptr,scoped_array,shared_array
- boost shared_ptr实现浅析
- shared_ptr 的简单实现
- c++ shared_ptr指针实现
- shared_ptr简单实现
- boost::shared_ptr 分析与实现
- boost::shared_ptr 分析与实现
- Java(8):基础算法(2)--冒泡排序算法 + 选择排序算法 + 插入排序算法
- MarkDown编辑器下载以及基本语法
- How to write mutiple graphs in a run in tensorflow
- 阿里开源Zeus概述
- java并发编程实战.pdf免费下载
- 模拟实现shared_ptr
- E: 软件包 ffmpeg 没有可供安装的候选者
- 【redis】无法连接linux上的redis
- 软件测试缺陷等级划分
- leetcode.array--78. Subsets
- 类的组合以及类的初始化顺序
- zookeeper3.4.8 +hbase1.2.6配置详细步骤
- 中共五大
- 简单的JDBC连接数据库