利用模板和C++11特性实现的智能指针-作用同share_ptr
根据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的更多相关文章
- C++11特性 - Smart Pointers 智能指针
已经有成千上万的文章讨论这个问题了,所以我只想说:现在能使用的,带引用计数,并且能自动释放内存的智能指针包括以下几种: unique_ptr: 如果内存资源的所有权不需要共享,就应当使 ...
- 智能指针类模板(中)——Qt中的智能指针
Qt中的智能指针-QPointer .当其指向的对象被销毁时,它会被自动置空 .析构时不会自动销毁所指向的对象-QSharedPointer .引用计数型智能指针 .可以被自由的拷贝和赋值 .当引用计 ...
- C++的优秀特性6:智能指针
(转载请注明原创于潘多拉盒子) 智能指针(Smart Pointer)是C++非常重要的特性.考虑如下一段使用简单指针(Plain Pointer)的代码: A* a = new A(); B* b ...
- 智能指针类模板(上)——STL中的智能指针
智能指针类模板智能指针本质上就是一个对象,它可以像原生指针那样来使用. 智能指针的意义-现代C++开发库中最重要的类模板之一-C++中自动内存管理的主要手段-能够在很大程度上避开内存相关的问题 1.内 ...
- 【C++11新特性】 C++11智能指针之shared_ptr
C++中的智能指针首先出现在“准”标准库boost中.随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象.在新标准中,主要提供了shared_p ...
- C++11中智能指针的原理、使用、实现
目录 理解智能指针的原理 智能指针的使用 智能指针的设计和实现 1.智能指针的作用 C++程序设计中使用堆内存是非常频繁的操作,堆内存的申请和释放都由程序员自己管理.程序员自己管理堆内存可以提高了程序 ...
- 引用内部函数绑定机制,R转义字符,C++引用,别名,模板元,宏,断言,C++多线程,C++智能指针
1.引用内部函数绑定机制 #include<iostream> #include<functional> usingnamespacestd; usingnamespac ...
- C++新特性---智能指针
智能指针: 为什么需要智能指针? 1. malloc出来的空间,没有进行释放,存在内存泄漏的问题. 2. 异常安全问题.如果在malloc和free之间如果存 ...
- c++11之智能指针
在c++98中,智能指针通过一个模板“auto_ptr”来实现,auto_ptr以对象的方式来管理堆分配的内存,在适当的时间(比如析构),释放所获得的内存.这种内存管理的方式只需要程序员将new操作返 ...
随机推荐
- glib系列1 编译
1 安装 meson glib 使用 meson进行构建,需要python3,首先下载meson, meson/meson.py 就是程序本身了,可以直接运行 git clone https://gi ...
- (七)springmvc之ModelAttribute注解
一.没有使用@ModelAttribute的Controller方法. @RequestMapping("/save") public String save(User user) ...
- ASP.NET Core 中的脚本标记帮助程序
官网地址:https://docs.microsoft.com/zh-cn/aspnet/core/mvc/views/tag-helpers/built-in/script-tag-helper?v ...
- 【原创】大叔经验分享(89)docker启动openjdk执行jmap报错
docker启动openjdk后,可以查看进程 # docker exec -it XXX jps 10 XXX.jar 可见启动的java进程id一直为10,然后可以执行jvm命令,比如 # doc ...
- QTabWidget标签实现双击关闭(转)
重载了QTabWidget(由于tabBar()是protected),这样就可以获取到标签了. 1 class Tab : public QTabWidget 2 { 3 Q_OBJECT 4 pu ...
- Django迁移
Django迁移 学习目标: 如何在不编写任何SQL语句的情况下创建数据库表 更改model如何自动修改数据库 如何还原对数据库所做的更改 迁移能解决的问题 如果你不了解Django或者WEB开发,肯 ...
- pymsql及事务
MySQL知识点补充 1.去重 distinct select distinct name,age from t1; # 针对查找出来的结果整行(记录)进行去重,也就是相同行只保存一个 注意点:dis ...
- JQuery的事件处理、Jason
事件的处理: <body> <div id="aa" style="width:100px; height:100px; background-colo ...
- SVN配置使用及移植
使用svn作为配置管理工具及其普遍的用于项目开发中,网上有很多关于svn的原理介绍及命令行管理教程.这里仅仅分享下个人配置及使用的过程,不通过命令行,可简单的上手操作.如有遗漏欢迎留言交流. 配置及使 ...
- string+DP leetcode-4.最长回文子串
5. Longest Palindromic Substring 题面 Given a string s, find the longest palindromic substring in s. Y ...