【智能指针的延伸】两种定制删除器的实现方法
来源:互联网 发布:2345好压mac版 编辑:程序博客网 时间:2024/05/29 07:46
所谓定制删除器,就是向目标模板提供一个可以自由选择析构的接口,这样做的好处就是可以使智能指针模板不再只能单独管理内存,我们还可以用它管理文件指针之类的东西。其实现方法有两种,这里我们以share指针为例。
1.传递一个类的模板参数并给出缺省值,模板中将该类设定为成员变量,通过该类(删除类)的实例化,调用类中所存的删除方法进行删除。
代码如下:
#include<iostream>template <class T>struct Del{void operator ()(const T*ptr){if (ptr){delete ptr;}}};struct Free{void operator() (void* ptr){cout << "free:" << ptr << endl;free(ptr);}};struct Close{void operator()(void *ptr){cout << "Close" << endl;fclose((FILE*)(ptr));}};template<class T, class Deleter = Del<T>>class SharedPtr{public:SharedPtr(T* ptr):_ptr(ptr), _pCount(new long(1)){}SharedPtr(T* ptr, Deleter del):_ptr(ptr), _pCount(new long(1)), _del(del){}~SharedPtr(){_Release();}SharedPtr(const SharedPtr<T>& sp):_ptr(sp._ptr), _pCount(sp._pCount){++(*_pCount);}//SharedPtr<T>& operator=(const SharedPtr<T>& sp)//传统写法//{//if (this != &sp)//{//this->_Release();//_pCount = sp._pCount;//_ptr = sp._ptr;//++(*_pCount);//}//return *this;//}SharedPtr<T>& operator=(SharedPtr<T> sp){swap(_ptr, sp._ptr);swap(_pCount, sp._pCount);return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}T* GetPtr(){return _ptr;}long GetCount(){return *_pCount;}protected:void _Release(){if (--(*_pCount) == 0){//delete _ptr;_del(_ptr);delete _pCount;}}protected:T* _ptr;long* _pCount;Deleter _del;};
测试用例如下:
void Test2(){SharedPtr<int> sp1(new int(1));SharedPtr<int, Free> sp2((int*)malloc(sizeof(int)* 10), Free());SharedPtr<FILE, Close>sp3 = std::fopen("Test.txt", "w");}
2.在智能指针的模板中,添加函数指针变量,通过构造函数确定所传递的函数指针,用该指针所指向的方法进行删除。
代码如下:
void free(){cout << "void free()" << endl;}void del(){cout << "void del()" << endl;}void close(){cout << "void close()" << endl;}template<class T>class SharedPtr{public:SharedPtr(T* ptr):_ptr(ptr), _pCount(new long(1)){}SharedPtr(T* ptr, void(*p)()):_ptr(ptr), _pCount(new long(1)), del(p){}~SharedPtr(){_Release();}SharedPtr(const SharedPtr<T>& sp):_ptr(sp._ptr), _pCount(sp._pCount){++(*_pCount);}SharedPtr<T>& operator=(SharedPtr<T> sp){swap(_ptr, sp._ptr);swap(_pCount, sp._pCount);return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}T* GetPtr(){return _ptr;}long GetCount(){return *_pCount;}protected:void _Release(){if (--(*_pCount) == 0){del();delete _pCount;}}protected:T* _ptr;long* _pCount;void(*del)();};
测试用例如下:
void Test1(){void(*p)();int a = 0;SharedPtr<int>p1(&a, close);}
如有什么不足或疑问,希望留言一起探讨,如有不择也希望批评指正。
本文出自 “pawnsir的IT之路” 博客,请务必保留此出处http://10743407.blog.51cto.com/10733407/1757336
0 0
- 【智能指针的延伸】两种定制删除器的实现方法
- 智能指针的简单实现及两种误用方式
- 智能指针的两种实现(引用计数)
- 函数指针实现的两种方法
- 智能指针(模拟实现auto_ptr,shared_ptr,scooeptr 以及定制删除器c++ 实现)
- 最简单的智能指针实现方法
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针的实现
- 智能指针(下)-----boost库智能指针,定制删除器、循环引用
- 模拟实现定制删除器的SharedPtr
- C++ - 智能指针(smarter pointer)自定义删除器(deleter) 的方法 详解 及 代码
- 实战c++中的智能指针unique_ptr系列-- unique_ptr的get_deleter方法(自定义删除器)
- repeater删除按钮的两种实现方法
- 智能指针的模拟实现shared_ptr 循环引用 定置删除器
- 智能指针的简单实现
- 【代码】模板实现动态线性表(无类型萃取)
- 【技巧】通过适配器模式完成栈的数据结构
- 【代码】模板动态线性表&类型萃取
- 【干货】share智能指针的模拟实现
- 【一张大大的图片】类和对象知识点总结
- 【智能指针的延伸】两种定制删除器的实现方法
- 【干货】容器适配器实现两个栈模拟队列
- 【代码】C++实现广义表及其测试用例
- 【代码】C++实现二叉树基本操作及测试用例
- 二叉树的线索化算法思想详解
- 【代码】c++堆的简单实现
- 堆排序算法思路详解
- 【代码】key-value模式下的哈希二次探测与简单的哈希类的实现
- 【代码】稀疏矩阵的压缩存储与转置算法