根据C++11特性实现,基本上实现了同SharePtr同样的功能,有时间继续优化。之前一直以为引用计数是一个静态的int类型,实际上静态值是不可以的。之前项目中总是不太习惯使用智能指针。通过自实现的方式,充分了解了智能指针的实现。

template <class T>
class SmartPtr
{
public:
SmartPtr(T * pointee=NULL)
:_pointee(pointee),_useCount(NULL){
if(_pointee)
{
_useCount = new unsigned int();
*_useCount = ;
cout<<"create a new smart pointer"<<endl;
}
}
SmartPtr(T * pointee,const std::function<void(T*)> deleter)
:_pointee(pointee),_useCount(NULL),_deleter(deleter){
if(_pointee)
{
_useCount = new unsigned int();
*_useCount = ;
cout<<"create a new smart pointer"<<endl;
}
} SmartPtr(const SmartPtr &another)
:_pointee(NULL),_useCount(NULL)
{
if(another._useCount)
{
_useCount = another._useCount;
(*_useCount)++;
_pointee = another._pointee;
if(another._deleter)
_deleter = another._deleter;
}
} ~SmartPtr()
{
cout<<"SmartPtr delete"<<endl;
if(_useCount){
if(--(*_useCount) == ){
if(_deleter)
_deleter(_pointee);
else
delete _pointee;
delete _useCount;
}
_useCount = NULL;
_pointee = NULL;
}
}
SmartPtr &operator=(const SmartPtr &another)
{
if((this != &another) && (_useCount !=NULL || another._useCount!=NULL))
{
if(_useCount ==NULL && another._useCount!=NULL)
{
_useCount = another._useCount;
++(*_useCount);
_pointee = another._pointee;
}
else if(_useCount !=NULL && another._useCount==NULL)
{
if(--(*_useCount) == ){
if(_deleter){
_deleter(_pointee);
}
else
delete _pointee;
delete _useCount;
}
_pointee = NULL;
_useCount = NULL;
}
else if(_useCount !=NULL && another._useCount!=NULL)
{
if(--(*_useCount) == ){
if(_deleter)
_deleter(_pointee);
else
delete _pointee;
delete _useCount;
}
_useCount = another._useCount;
++(*_useCount);
_pointee = another._pointee;
}
}
return *this; }
bool operator!() const
{
return !_pointee?true:false;
} operator void*(void) const
{
return _pointee;
} T * operator->() const
{
return _pointee;
} T & operator*() const
{
return *_pointee;
}
T * get()
{
return _pointee;
}
bool unique()
{
if(!_useCount)
return false;
return *_useCount==?true:false;
}
constexpr long use_count() noexcept
{
if(!_useCount)
return ;
return *_useCount;
}
// void swap();
void reset()
{
if(_useCount){
if(--(*_useCount)==)
{
if(_deleter){
_deleter();
memset(&_deleter,0x00,sizeof(decltype(_deleter)));
}
else
delete _pointee;
delete _useCount;
}
_pointee = NULL;
_useCount = NULL; }
}
void reset(T * pt)
{
if(--(*_useCount)==)
{
if(_deleter){
_deleter(_pointee);
memset(&_deleter,0x00,sizeof(decltype(_deleter)));
cout<<"memset _deleter"<<endl;
} else{
delete _pointee;
cout<<"delete the number"<<endl;
}
*_useCount = ;
}
else{
_useCount = new unsigned int(); }
_pointee = pt;
} void reset(T * pt,const std::function<void(T*)>& deleter)
{
if(--(*_useCount)==)
{
if(_deleter)
_deleter(_pointee);
else
delete _pointee; _deleter = deleter;
*_useCount = ;
}
else{
_useCount = new unsigned int(); }
_pointee = pt;
} private:
unsigned int *_useCount;
T * _pointee;
std::function<void(T*)> _deleter;
}; class Test
{
public:
Test(){cout<<"Test()"<<endl;}
Test(const Test &another){cout<<"Test(const Test &another)"<<endl;}
Test & operator=(const Test &another){
cout<<"Test & operator=(const Test &another)"<<endl;
return *this;
}
~Test(){
cout<<"~Test()"<<endl;
}
}; void funcdelet(Test *t)
{
delete[] t;
cout<<"costmos deleter"<<endl;
}
int main()
{
// SmartPtr<Test> pt1(new Test[10],[](Test *t){ delete[] t;cout<<"lamda function called"<<endl;}); // SmartPtr<Test> pt3(new Test,funcdelet);
// pt3 = pt1; SmartPtr<Test> pt2(new Test[],[](Test *t){ cout<<"lamda function called"<<endl; delete[] t;}); cout<<pt2.use_count()<<endl; pt2.reset(new Test,[](Test *t){delete t,cout<<"customs define"<<endl;});
cout<<"------------------"<<endl;
cout<<pt2.use_count()<<endl; }

最新文章

  1. H5唤起APP一些坑
  2. webstorm运行到服务器(Apache)
  3. HDU 3374 String Problem (KMP+最大最小表示)
  4. MTK6577+Android4.04编译
  5. android系统平台显示驱动开发简要:LCD驱动调试篇『四』
  6. nginx 常用的 URL 重写方法
  7. Arcengine 二次开发添加右键菜单
  8. 纯css3打造瀑布流布局
  9. 【算法】LeetCode算法题-Remove Element
  10. mybatis-servlet.xml配置SpringMVC样板
  11. Django-website 程序案例系列-1 最简单的web服务器
  12. Android studio3.1.3 打包jar,混淆
  13. ERROR: iterator not incrementable || iterator not decrementable
  14. 聊下图片滤镜,手机上的,lookup table(颜色查找表
  15. 尺寸单位em,rem,vh,vw
  16. NIO中几个非常重要的技术点
  17. 005——数组(五)array_diff_ukey()array_diff_uassoc()array_intersect()array_intersect_assoc()array_intersect_key()array_intersect_ukey()array_intersect_uassoc()
  18. 读懂IL代码就这么简单 ---- IL系列文章
  19. 谷歌浏览器和火狐浏览器设置跨域和https、http混用 Chrome
  20. Stars in Your Window(线段树求最大矩形交)

热门文章

  1. c++学习---迭代器
  2. 怎样理解prototype对象的constructor属性
  3. Spring 后台方法 重定向 与 转发
  4. Java单例设计模式和多例设计模式
  5. .net core 依赖注入在特性中的应用
  6. 数组的新API
  7. ccs编译.lib
  8. graph Laplacian 拉普拉斯矩阵
  9. Centos7下安装Elasticsearch 5.6.6
  10. PyQt5多个GUI界面设计