Counter类 Counter对象的目地就是用来申请一个块内存来存引用计数。shareCount是SharedPtr的引用计数,weakCount是弱引用计数。 当shareCount为0时,删除T*对象。 当weakCount为0同时shareCount为0时,删除Counter*对象。 Counter实现如下: class Counter { public: int shareCount = 0; int weakCount = 0; }; SharedPtr类 主要的成员函数包括: 默认构造函数 参数为T*的explicit单参数构造函数 参数为WeakPtr&的explicit单参数构造函数 拷贝构造函数 拷贝赋值函数 析构函数 隐式类型转换操作符 operator bool () operator -> () operator * () SharedPtr实现如下: template<class T> class WeakPtr; template<class T> class SharedPtr { public: friend class WeakPtr<T>; //方便weak_ptr与share_ptr设置引用计数和赋值。 SharedPtr() : m_pResource(nullptr) , m_pCounter(new Counter()) { m_pCounter->shareCount = 1; } explicit SharedPtr(T* pResource = nullptr) : m_pResource(pResource) , m_pCounter(new Counter()) { m_pCounter->shareCount = 1; } SharedPtr(const WeakPtr<T>& other) // 供WeakPtr的lock()使用 : m_pResource(other.m_pResource) , m_pCounter(other.m_pCounter) { if (0 == m_pCounter->shareCount) m_pResource = nullptr; } SharedPtr(const SharedPtr<T>& other) : m_pResource(other->m_pResource) , m_pCounter(other->m_pCounter) { ++(m_pCounter->shareCount); // 增加引用计数 } SharedPtr<T>& operator = (const SharedPtr<T>& other) { if (this == &other) return *this; release(); m_pCounter = other.m_pCounter; m_pResource = other.m_pResource; ++(m_pCounter->shareCount); // 增加引用计数 return *this; } ~SharedPtr() { release(); } T& operator bool() { return m_pResource != nullptr; } T& operator * () { // 如果nullptr == m_pResource,抛出异常 return *m_pResource; } T* operator -> () { return m_pResource; } private: void release() { // T*肯定由SharedPtr释放,Counter*如果没有WeakPtr,也由SharedPtr释放 --m_pCounter->shareCount; if (0 == m_pCounter->shareCount) { delete m_pResource; m_pResource = nullptr; if (0 == m_pCounter->weakCount) { delete m_pCounter; m_pCounter = NULL; } } } public: T* m_pResource = nullptr; Counter* m_pCounter = nullptr; }; WeakPtr类 主要的成员函数包括: 默认构造函数 参数为SharedPtr&的explicit单参数构造函数 拷贝构造函数 拷贝赋值函数 析构函数 lock()函数:取指向的SharePtr,如果未指向任何SharePtr,或者已被析构,返回指向nullptr的SharePtr expired()函数:是否指向SharePtr,如果指向Share Ptr其是否已经析构 release()函数 WeakPtr实现如下: template<class T> class WeakPtr { public: friend class SharedPtr<T>;//方便weak_ptr与share_ptr设置引用计数和赋值。 WeakPtr() m_pResource(nullptr) , m_pCounter(new Counter()) { m_pCounter->weakCount = 1; } WeakPtr(SharedPtr<T>& other) : m_pResource(other.m_pResource) , m_pCounter(other.m_pCounter) { ++(m_pCounter->weakCount); } WeakPtr(WeakPtr<T>& other) : m_pResource(other.m_pResource) , m_pCounter(other.m_pCounter) { ++(m_pCounter->weakCount); } WeakPtr<T>& operator = (WeakPtr<T>& other) { if (this == &other) return *this; release(); m_pCounter = other.m_pCounter; m_pResource = other.m_pResource; ++m_pCounter->weakCount; return *this; } WeakPtr<T>& operator =(SharedPtr<T>& other) { release(); m_pCounter = other.m_pCounter; m_pResource = other.m_pCounter; ++m_pCounter->weakCount; // 增加弱引用计数 return *this; } ~WeakPtr() { release(); } SharedPtr<T> lock() { return SharedPtr<T>(*this); } bool expired() { if (m_pCounter != nullptr && m_pCounter->shareCount != 0) return false; return true; } private: void release() { --m_pCounter->weakCount; if (0 == m_pCounter->weakCount && 0 == m_pCounter->shareCount) // 必须都为0才能删除 { delete m_pCounter; m_pCounter = NULL; } } private: T* m_pResource; // 可能会成为悬挂指针 Counter* m_pCounter; };