根据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. go for range 可以方便的对slice 切片或者 map 进行迭代循环

    package main import (     "fmt"     "math/rand"     "time" ) func main ...

  2. java 获取json字符串中key对应的值

    用到了Gson的JsonParser maven项目引入 <dependency> <groupId>com.google.code.gson</groupId> ...

  3. Mysql高可用集群环境介绍

    MySQL高可用集群环境搭建 01.MySQL高可用环境方案 02.MySQL主从复制原理 03.MySQL主从复制作用 04.在Linux环境上安装MySQL 05.在MySQL集群环境上配置主从复 ...

  4. (java实现)双向循环链表

    什么是双向循环链表 在了解双向循环链表之前,如果对链表还没有一个清晰的概念,建议你看看单链表和单向循环链表,这有利于你更好的理解下面的内容.(废话有点多[逃] 相比单链表,双向循环链表是一个更加复杂的 ...

  5. (三)springmvc之注解的基本使用

    一.@Controller @Controller 标记一个类是Controller 二.RequestMapping  地址映射 2.1 Value的操作. 注解在类上面    (父)       ...

  6. 利用jwt生成token,用于http请求身份验证

    前段时间在做移动端接口过程中,考虑到安全性,所有移动端发送请求(除了登录请求)过程中进行token有效验证. 1.利用jwt生成token a.导入jwt相关包 <!-- jwt --> ...

  7. Java Web 深入分析(9) Session 和 Cookie

    前言: session 和cookie都是为了保持服务器和客户端之间交互状态.如果一天的PV有几亿,而一个cookie占200个字节但是也会占用很多带宽?所以大访问量就引用session,但是几百台服 ...

  8. 关于困惑已久的var self=this的解释

    首先说下this这个对象的由来(属于个人理解):每个函数在定义被ECMAScript解析器解析时,都会创建两个特殊的变量:this和arguments,换句话说,每个函数都有属于自己的this对象,这 ...

  9. Lua table直接索引VS缓存索引性能测试小示例

    local p = {} p.t = {} p.t.p = {} p.t.p.t = {} p.t.p.t.p = {} p.t.p.t.p.t = {} p.t.p.t.p.t.p = {} p.t ...

  10. xposed获取类的属性成员

    XposedBridge.log("开始获取属性:"); Field[] fields = param.thisObject.getClass().getDeclaredField ...