template<typename SmartPointer,typename Type> class PtrCount{ friend SmartPointer; PtrCount(Type* _p) :p(_p), use(1){} ~PtrCount(){ delete p; } Type *p; size_t use; }; template<typename Type> class SmartPtr{ public: SmartPtr(Type* p) :ptrCnt(new PtrCount<SmartPtr, Type>(p)){}; SmartPtr(const SmartPtr& orig):ptrCnt(orig.ptrCnt){ ++ptrCnt->use; } SmartPtr& operator=(const SmartPtr& rhs){ ++rhs.ptrCnt->use; if (--ptrCnt->use == 0) delete ptrCnt; ptrCnt = rhs.ptrCnt; return *this; } ~SmartPtr(){ if (--ptrCnt->use == 0) delete ptrCnt; } private: PtrCount<SmartPtr, Type> *ptrCnt; };
class Object; class SmartPointer; class Counter { friend class SmartPointer; public: Counter() { ptr = NULL; cnt = 0; } Counter(Object* p) { ptr = p; cnt = 1; } ~Counter() { delete ptr; } private: Object* ptr; int cnt; }; class SmartPointer { public: SmartPointer(Object* p) { ptr_counter = new Counter(p); } SmartPointer(const SmartPointer &sp) { ptr_counter = sp.ptr_counter; ++ptr_count->cnt; } SmartPointer& operator=(const SmartPointer &sp) { ++sp.ptr_counter->cnt; --ptr_counter->cnt; if (ptr_counter->cnt == 0) { delete ptr_counter; } ptr_counter = sp.ptr_counter; } ~SmartPointer() { - -ptr_counter->cnt; if (ptr_counter->cnt == 0) { delete ptr_counter; } } private: Counter* ptr_counter; };
/** * 智能指针,简单来讲是使用引用计数的方法,来跟踪监控指针。当引用计数为0时就delete 所跟踪的目标指针,释放内存 * 智能指针将一个指针封装到一个类中,当调用智能指针的拷贝构造函数时,将引用计数+1(因为新创建的智能指针也引用了目标指针) * 重载智能指针的赋值操作符,等号左边的对象引用计数-1,右边的对象引用计数+1,右边的目标指针和引用计数赋值给左边的对象 * 智能指针的析构函数将引用计数-1,并判断是否为0,如果是的话delete 目标指针。 */ template <class T> class smart_ptr { private: T* ptr; int* count; public: smart_ptr(T* p=0):ptr(p),count(new int(0)) { if(p) *count=1; } smart_ptr(const smart_ptr& src) { ++*src.count; count=src.count; ptr=src.ptr; } smart_ptr& operator =(const smart_ptr& src) { --*count; if(*count==0) delete ptr; count=src.count; ++*count; ptr=src.ptr; } const smart_ptr& operator =(const smart_ptr& src) const { --*count; if(*count==0) delete ptr; count=src.count; ++*count; ptr=src.ptr; } T* operator ->() { return ptr; } ~smart_ptr() { --*count; if(*count==0) { delete ptr; delete count; ptr=NULL; count=NULL; } } };
#include <iostream> using namespace std; // 具体操作类 class Object { public: int a; int b; }; // 操作函数 void Process(Object* p) { // do something }; class Counter { friend class SmartPointer; public: Counter() { ptr=NULL; cnt=0; } Counter(Object* p) { ptr=p; cnt=1; } ~Counter() { delete ptr; } private: Object* ptr; int cnt; } class SmartPointer { public: SmartPointer(Object* p) { ptr_pointer=new Counter(p); } SmartPointer(const SmartPointer &sp) { ptr_counter=sp.ptr_counter; ++ptr_counter->cnt; } SmartPointer& operator=(const SmartPointer) { ++sp.ptr_counter->cnt; --ptr_counter.cnt; if(ptr_counter.cnt==0) { delete ptr_counter; } ptr_counter=sp.ptr_counter; } ~Smartpointer() { --ptr_counter->cnt; if(ptr_counter->cnt==0) { delete ptr_counter; } } private: Counter* ptr_counter; }; int main() { SmartPointer p(new Object()); Process(p); }2 C++智能指针主要有: unique_ptr;shared_ptr;weak_ptr三种
/* *my shared_ptr: share_ptr *author:ERIC *blog:http://www.ilovecpp.com *time:2015-5-27 23:56:43 */ template <typename T> class share_ptr { private: T* __ptr; int* __pcounts; public: share_ptr(T* p= NULL); share_ptr(const share_ptr<T>& src); share_ptr& operator=(share_ptr<T>& src); ~share_ptr(); operator bool() const;//支持if(p)形式 T* operator-> () const; T& operator*() const; T* get() const; int use_counts() const;//返回引用计数 bool unique() const;//当前智能指针是否唯一 void swap(share_ptr& rhs);//交换,成员函数 template <typename Type>//友元函数,交换两个智能指针 friend void swap(share_ptr<Type>& lhs,share_ptr<Type>& rhs); }; template<typename T> share_ptr<T>::share_ptr(T* p) :__ptr(p),__pcounts(new int(0)) { if(__ptr) *__pcounts = 1; } template<typename T> share_ptr<T>::~share_ptr() { --*__pcounts; if(*__pcounts == 0) {//空智能指针这里delete __ptr也安全,delete NULL; delete __pcounts; delete __ptr; } } /*__ptr(new T(src)) 很重要 ,如果直接__ptr(new T) *会由于T类没有默认构造函数而出错 *测试的时候才发现这个问题的 */ template<typename T> share_ptr<T>::share_ptr (const share_ptr<T>& src) :__pcounts(new int),__ptr(new T(src)) { ++*src.__pcounts; __ptr = src.__ptr; __pcounts = src.__pcounts; } template <typename T> share_ptr<T>& share_ptr<T>::operator= (share_ptr<T>& src) { --*__pcounts; //如果是空智能指针的话 __pcounts == -1,那块内存也得释放 if(*__pcounts == 0 || *__pcounts == -1) delete __pcounts; ++*src.__pcounts; __ptr = src.__ptr; __pcounts = src.__pcounts; return *this; } //支持if(p)这样的操作 template<typename T> share_ptr<T>::operator bool() const { return __ptr; } template<typename T> T* share_ptr<T>::operator->() const { return __ptr; } template<typename T> T& share_ptr<T>::operator*() const { return *__ptr; } template<typename T> T* share_ptr<T>::get() const { return __ptr; } template<typename T> int share_ptr<T>::use_counts() const { return *__pcounts; } template<typename T> bool share_ptr<T>::unique() const { if(*__pcounts == 1) return true; else return false; } template<typename T> void share_ptr<T>::swap(share_ptr<T>& rhs) { T* tmpPtr = rhs.__ptr; rhs.__ptr = __ptr; __ptr = tmpPtr; int* tmpPcounts = rhs.__pcounts; rhs.__pcounts = __pcounts; __pcounts = tmpPcounts; } template<typename T> void swap(share_ptr<T>& lhs,share_ptr<T>& rhs) { T* tmpPtr = rhs.__ptr; rhs.__ptr = lhs.__ptr; lhs.__ptr = tmpPtr; int* tmpPcounts = rhs.__pcounts; rhs.__pcounts = lhs.__pcounts; lhs.__pcounts = tmpPcounts; } //c++11 make_shared<T>(args) template<typename T> share_ptr<T> make_share(T args) { return new T(args); }