根据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; }

利用模板和C++11特性实现的智能指针-作用同share_ptr的更多相关文章

  1. C++11特性 - Smart Pointers 智能指针

    已经有成千上万的文章讨论这个问题了,所以我只想说:现在能使用的,带引用计数,并且能自动释放内存的智能指针包括以下几种:         unique_ptr: 如果内存资源的所有权不需要共享,就应当使 ...

  2. 智能指针类模板(中)——Qt中的智能指针

    Qt中的智能指针-QPointer .当其指向的对象被销毁时,它会被自动置空 .析构时不会自动销毁所指向的对象-QSharedPointer .引用计数型智能指针 .可以被自由的拷贝和赋值 .当引用计 ...

  3. C++的优秀特性6:智能指针

    (转载请注明原创于潘多拉盒子) 智能指针(Smart Pointer)是C++非常重要的特性.考虑如下一段使用简单指针(Plain Pointer)的代码: A* a = new A(); B* b ...

  4. 智能指针类模板(上)——STL中的智能指针

    智能指针类模板智能指针本质上就是一个对象,它可以像原生指针那样来使用. 智能指针的意义-现代C++开发库中最重要的类模板之一-C++中自动内存管理的主要手段-能够在很大程度上避开内存相关的问题 1.内 ...

  5. 【C++11新特性】 C++11智能指针之shared_ptr

    C++中的智能指针首先出现在“准”标准库boost中.随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象.在新标准中,主要提供了shared_p ...

  6. C++11中智能指针的原理、使用、实现

    目录 理解智能指针的原理 智能指针的使用 智能指针的设计和实现 1.智能指针的作用 C++程序设计中使用堆内存是非常频繁的操作,堆内存的申请和释放都由程序员自己管理.程序员自己管理堆内存可以提高了程序 ...

  7. 引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针

     1.引用内部函数绑定机制 #include<iostream> #include<functional> usingnamespacestd; usingnamespac ...

  8. C++新特性---智能指针

    智能指针:     为什么需要智能指针?         1. malloc出来的空间,没有进行释放,存在内存泄漏的问题.          2. 异常安全问题.如果在malloc和free之间如果存 ...

  9. c++11之智能指针

    在c++98中,智能指针通过一个模板“auto_ptr”来实现,auto_ptr以对象的方式来管理堆分配的内存,在适当的时间(比如析构),释放所获得的内存.这种内存管理的方式只需要程序员将new操作返 ...

随机推荐

  1. Error:Could not find method google() for arguments [] on repository container

    Error:Could not find method google() for arguments [] on repository container. Consult IDE log for m ...

  2. unittest之二makeSuite\testload\discover及测试报告teseReport

    测试套件suite除了使用addTest以外,还有使用操作起来更更简便的makeSuite\testload\discover 1.makeSuite,创建测试套件,传的参数是要执行的测试用例所在的类 ...

  3. SSH框架CRUD+树形菜单案例

    今天结合了案例来写ssh的增删改查 表设计 t_ssh_tree t_vue_user  book 核心配置文件  struts-base.xml <?xml version="1.0 ...

  4. 通过json_set函数,来修改data字段的值

    SELECT REPLACE(json_extract(param,'$.payFundAcc'),'"','') from t_external_trade_event where sta ...

  5. 红帽linux系统开机自启动脚本。

    其实很多东西在最后完成以后会觉得也就那样,有意思的是探究的过程. 前段时间老板要求把一个程序做成linux系统开机自启动脚本的模式. 首先你需要写一个脚本. 我这边建立了一个.sh的脚本,就是用脚本启 ...

  6. hdu 1114需要装满的完全背包 重点是背包初始化的问题

    .,. 最近在看背包九讲 所以就刷了一下背包的题目 这道题目是一个典型的完全背包问题 而且要求满包 在这里 我就简单整理一下背包初始化问题吧 对于没有要求满包的问题 也就是背包可以不取满的问题 在背包 ...

  7. 基于【 Docker】五 || maven私服环境搭建

    1.Maven  Nexus私服的原理 为了节省带宽和时间,在局域网内架设一个私有的仓库服务器,用其代理所有外部的远程仓库.当本地Maven项目需要下载构件时,先去私服请求,如果私服没有,则再去远程仓 ...

  8. JavaScript程序块初始练习

    由语句语句块,函数,对象,方法,属性构成.通过顺序,分支,循环三种基本程序控制结构来进行编程. 来一段小代码: <!DOCTYPE html> <html> <head& ...

  9. Aop动态代理和cglib

    一般我们使用Aop对象时,常用动态代理模式,即是采用映射一个相同的类在此基础上进行前置后置操作. 动态代理多是采用原类实现父类接口,然后动态代理一个和原类相同的双胞胎兄弟类来实现映射. 父类 publ ...

  10. 12_Azkaban案例实践5_Command操作Hive脚本任务

    HIVE脚本任务 hadoop fs -mkdir -p /aztest/hiveinput hadoop fs -put az.data /aztest/hiveinput/ l 创建job描述文件 ...