先简单介绍一下仿函数是什么:仿函数(functor),就是使一个类的使用看上去像一个函数。其实现就是类中实现一个operator(),这个类就有了类似函数的行为,就是一个仿函数类了。
简单定义了一下删除器需要的仿函数
#include<iostream>
#include<cstdlib>
#include<boost\shared_ptr.hpp>
using namespace std;
struct Fclose
{
void operator()(void *ptr)
{
fclose((FILE*)ptr);
cout<<"close file"<<endl;
}
#include<cstdlib>
#include<boost\shared_ptr.hpp>
using namespace std;
struct Fclose
{
void operator()(void *ptr)
{
fclose((FILE*)ptr);
cout<<"close file"<<endl;
}
};
struct Free
{
void operator()(void *ptr)
{
free(ptr);
cout<<"free"<<endl;
}
};
struct Delete
{
void operator()(void *ptr)
{
delete ptr;
cout<<"delete"<<endl;
}
};
struct DeleteArray
{
void operator()(void *ptr)
{
delete[]ptr;
cout<<"deletle array"<<endl;
}
};
struct Free
{
void operator()(void *ptr)
{
free(ptr);
cout<<"free"<<endl;
}
};
struct Delete
{
void operator()(void *ptr)
{
delete ptr;
cout<<"delete"<<endl;
}
};
struct DeleteArray
{
void operator()(void *ptr)
{
delete[]ptr;
cout<<"deletle array"<<endl;
}
};
void test()
{
boost::shared_ptr<FILE> p1(fopen("test.txt","w"),Fclose()); //像调用函数一样使用
int *p=(int*)malloc(sizeof(int));
boost::shared_ptr<int> p2(p,Free());
int *p3=new int(1);
boost::shared_ptr<int> p3_ptr(p3,Delete());
char* p4=new char[4];
boost::shared_ptr<char> p4_ptr(p4,DeleteArray());
}
int main()
{
test();
{
boost::shared_ptr<FILE> p1(fopen("test.txt","w"),Fclose()); //像调用函数一样使用
int *p=(int*)malloc(sizeof(int));
boost::shared_ptr<int> p2(p,Free());
int *p3=new int(1);
boost::shared_ptr<int> p3_ptr(p3,Delete());
char* p4=new char[4];
boost::shared_ptr<char> p4_ptr(p4,DeleteArray());
}
int main()
{
test();
system("pause");
return 0;
}
return 0;
}
使用仿函数可以来定制删除器
#include<iostream>
#include<cstdlib>
#include<boost\shared_ptr.hpp>
using namespace std;
struct Fclose
{
void operator()(void *ptr)
{
fclose((FILE*)ptr);
cout<<"close file"<<endl;
}
#include<cstdlib>
#include<boost\shared_ptr.hpp>
using namespace std;
struct Fclose
{
void operator()(void *ptr)
{
fclose((FILE*)ptr);
cout<<"close file"<<endl;
}
};
struct Free
{
void operator()(void *ptr)
{
free(ptr);
cout<<"free"<<endl;
}
};
struct Delete
{
void operator()(void *ptr)
{
delete ptr;
cout<<"delete"<<endl;
}
};
struct DeleteArray
{
void operator()(void *ptr)
{
delete[]ptr;
cout<<"deletle array"<<endl;
}
};
struct Free
{
void operator()(void *ptr)
{
free(ptr);
cout<<"free"<<endl;
}
};
struct Delete
{
void operator()(void *ptr)
{
delete ptr;
cout<<"delete"<<endl;
}
};
struct DeleteArray
{
void operator()(void *ptr)
{
delete[]ptr;
cout<<"deletle array"<<endl;
}
};
template<typename T,typename D=Delete>
class SharedePtr
{
public:
SharedePtr(T* ptr)
:_ptr(ptr),
_pcount(new int(1)),
_del(D())
{}
~SharedePtr()
{
Release();
}
SharedePtr(SharedePtr<T,D>& sp)
{
_ptr=sp._ptr;
_pcount=sp._pcount;
++(*_pcount);
return *this;
}
SharedePtr<T,D>& operator=(SharedePtr<T,D> ap)
{
std::swap(ap._ptr,_ptr);
std::swap(ap._pcount,_pcount);
return *this;
}
T& operator*()
{
return *_ptr;
}
T* operator->()
{
return _ptr;
}
T& ues_count()
{
return (*_pcount);
}
private:
void Release()
{
if(--*_pcount==0)
{
_del(_ptr);
delete _pcount;
}
}
T* _ptr;
int* _pcount;
D _del;
{
return _ptr;
}
T& ues_count()
{
return (*_pcount);
}
private:
void Release()
{
if(--*_pcount==0)
{
_del(_ptr);
delete _pcount;
}
}
T* _ptr;
int* _pcount;
D _del;
};
int main()
{
SharedePtr<FILE,Fclose> ap(fopen("test.tex","w"));
SharedePtr<int> ap1(new int(1));
system("pause");
return 0;
}
{
SharedePtr<FILE,Fclose> ap(fopen("test.tex","w"));
SharedePtr<int> ap1(new int(1));
system("pause");
return 0;
}