根据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. 无服务架构在IOT的应用场景——使用函数工作流处理DIS数据

    在物联网领域,复杂性往往并非在于传感器,真正的复杂性在于各种传感器产生的大量数据,以及对这些数据的处理,所以开发者不得不花费大量的时间去构建和维护后端服务器来处理这样一个庞大的数据流.而在今天这个敏捷 ...

  2. internal关键字

    internal修饰符可以用于类型或成员,使用该修饰符声明的类型或成员只能在同一程集内访问,接口的成员不能使用internal修饰符. 就是使用internal的类只能在同一个项目中使用,不能在别的项 ...

  3. RESTful接口开发

    package com.aaaaaa.manager.controller; import org.springframework.beans.factory.annotation.Autowired ...

  4. JS原型对象的问题

    原型模式最大的问题是由其共享的本性所导致的.我们知道,原型中所有的成员是被很多实例共享的,这种共享对于函数非常合适.对于那些包含基本值的属性倒也说得过去,毕竟可以通过在实例上添加一个同名的属性来隐藏原 ...

  5. 踩坑记录-用koa-session设置session报错

    报如下错误: 原因 设置signed: true后,它就会寻找req.secret(一个秘钥字符串),进行加密 allen返回浏览器. const SESS_CONFIG = { key: 'kkb: ...

  6. Jmeter5.1——聚合报告参数分析

    Jmeter5.1——聚合报告参数分析 Label: 每个JMeter的element的Name值.例如HTTP Request的Name. Samples:发出请求的数量.如果线程组中配置的是线程数 ...

  7. 五、HashMap的使用 及其源码解析

    HashMap的底层实现原理?领接表(数组+链表)hash表数组+链表+红黑树 链表:查找慢 插入 删除快红黑树:查找快 插入 删除慢 HashMap是线程安全的吗?不是线程安全的 在什么情况下 ,是 ...

  8. 11.ForkJoinPool 分支/合并框架 (工作窃取)

    /*ForkJoinPool 分支/合并框架 (工作窃取)*/ Fork/Join 框架:就是在必要的情况下,将一个大任务,进行拆分(fork) 成若干个小任务(拆到给出的临界值为止),再将一个个的小 ...

  9. 【Day5】1.Request对象之Header伪装策略

    import urllib.request as ur import user_agent request = ur.Request( url='https://edu.csdn.net/', hea ...

  10. 01 Windows编程——Hello World

    源码 #include "stdafx.h" #include<Windows.h> int WINAPI WinMain(HINSTANCE hInst,HINSTA ...