赞
踩
基于C++11中shared_ptr源码改写。全部代码都已经过测试检验。
#ifndef _KSHAREDPTR_H_ #define _KSHAREDPTR_H_ #include <iostream> using std::cout; using std::cin; using std::endl; using std::auto_ptr; template <typename T> class KSharedPtr { private: T* m_ptr; int* m_count; public: explicit KSharedPtr(T* p = 0); //默认构造函数 KSharedPtr(KSharedPtr const& r); //复制构造函数 KSharedPtr& operator=(KSharedPtr const& r); //重载赋值运算符 explicit KSharedPtr(auto_ptr<T>& r); //用auto指针构造 KSharedPtr& operator=(auto_ptr<T>& r); //重载赋值运算符(通过auto) ~KSharedPtr(); //析构函数 T& operator*() const; //重载*指针操作 T* operator->() const; //重载->操作 bool operator==(KSharedPtr const& r); //重载运算符== bool operator!=(KSharedPtr const& r); //重载运算符!= T* get()const; //返回当前存储的指针 void reset(T* p = 0); //重置当前存储的指针 int getCount(); //获取引用数 }; //默认构造函数 template <typename T> KSharedPtr<T>::KSharedPtr(T* p /* = 0 */) { this->m_ptr = p; try { this->m_count = new int(1); } catch (...) { delete this->m_ptr; //申请失败,释放真实指针和引用计数的内存 this->m_ptr = nullptr; delete this->m_count; this->m_count = nullptr; } } //复制构造函数 template <typename T> KSharedPtr<T>::KSharedPtr(KSharedPtr const& r) { this->m_count = r.m_count; this->m_ptr = r.m_ptr; ++(*this->m_count); } //重载赋值运算符 template <typename T> KSharedPtr<T>& KSharedPtr<T>::operator =(KSharedPtr const& r) { this->m_count = r.m_count; this->m_ptr = r.m_ptr; ++(*this->m_count); return *this; } //用auto指针构造 template <typename T> KSharedPtr<T>::KSharedPtr<T>(auto_ptr<T>& r) { this->m_ptr = r.release(); this->m_count = new int(1); } //重载赋值运算符(通过auto) template <typename T> KSharedPtr<T>& KSharedPtr<T>::operator=(auto_ptr<T>& r) { this->m_ptr = r.release(); this->m_count = new int(1); return *this; } //析构函数 template <typename T> KSharedPtr<T>::~KSharedPtr() { if (--(*this->m_count) == 0) { this->m_ptr = nullptr; delete this->m_count; this->m_count = nullptr; } } //重载*指针操作 template<typename T> T& KSharedPtr<T>::operator*()const { return *this->m_ptr; } //重载->操作 template<typename T> T* KSharedPtr<T>::operator->()const { return this->m_ptr; } //重载比较运算符== template <typename T> bool KSharedPtr<T>::operator==(KSharedPtr const& r) { if (this->m_ptr == r.m_ptr) return true; else return false; } //重载比较运算符!= template <typename T> bool KSharedPtr<T>::operator!=(KSharedPtr const& r) { if (this->m_ptr != r.m_ptr) return true; else return false; } //返回当前存储的指针 template <typename T> T* KSharedPtr<T>::get() const { return m_ptr; } //获取引用数 template <typename T> int KSharedPtr<T>::getCount() { return *this->m_count; } //重置当前存储的指针 template <typename T> void KSharedPtr<T>::reset(T* p) { if (p == 0) { if (--(*this->m_count) == 0) { this->m_ptr = nullptr; delete this->m_count; this->m_count = nullptr; } } else { this->m_ptr = p; delete this->m_count; this->m_count = new int(1); } } #endif // _KSHAREDPTR_H_
#include "kSharedPtr.h" void test1() //测试构造函数 { int* a = new int(78); int b = 46; KSharedPtr<int> p1(a); KSharedPtr<int> p2(p1); KSharedPtr<int> p3 = p2; cout << *p3 << " " << p3.getCount(); } void test2() //测试auto转换相关 { int* a = new int(8); int* b = new int(25); auto_ptr<int> c(a); auto_ptr<int> d(b); KSharedPtr<int> p1(c),p2; p2 = d; cout << *p1 << " " << p1.getCount() << endl; cout << *p2 << " " << p2.getCount() << endl; } void test3() //测试析构函数 { int a = 67; KSharedPtr<int> p1(&a); KSharedPtr<int> p2(p1); KSharedPtr<int> p3 = p2; cout << *p3 << " " << p3.getCount() << endl; p2.~KSharedPtr(); p3.~KSharedPtr(); cout << *p3 << " " << p3.getCount() << endl; } void test4() //测试==、!= { int a = 67; int b = 90; KSharedPtr<int> p1(&a); KSharedPtr<int> p2(p1); KSharedPtr<int> p3(&b); if (p3 != p1) cout << 1 << endl; else cout << -1 << endl; } void test5() //测试get、reset { int a = 67; int b = 90; KSharedPtr<int> p1(&a); KSharedPtr<int> p2(p1); KSharedPtr<int> p3 = p2; cout << *p3.get() <<" "<< p3.getCount() << endl; p3.reset(); cout << *p3.get() << " " << p3.getCount() << endl; } int main() { test5(); system("pause"); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。