shared_ptr的简单实现(非线程安全)
主要包括以下成员函数:
构造函数
析构函数
拷贝构造函数
operator=()
operator*()
operator->()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include <iostream> #include <cassert>
using namespace std;
template<typename T> class shared_ptr{ private: size_t* p_count; T* ptr; public: explicit shared_ptr(T* _ptr = nullptr):ptr(_ptr){ if(ptr) p_count = new size_t(1); else p_count = new size_t(0); }
shared_ptr(const shared_ptr<T>& rhs){ ptr = rhs.ptr; p_count = rhs.p_count; (*p_count)++; }
shared_ptr<T>& operator=(const shared_ptr<T>& rhs){ if(this == &rhs) return *this;
if(ptr){ (*p_count)--; if((*p_count)==0){ delete ptr; delete p_count; } }
ptr = rhs.ptr; p_count = rhs.p_count; (*p_count)++; return *this; }
T& operator*(){ assert(ptr); return *ptr; }
T* operator->(){ assert(ptr); return ptr; }
size_t use_count() const { return *p_count; }
~shared_ptr(){ (*p_count)--; if((*p_count)==0){ delete ptr; delete p_count; } } };
class test{ public: int a = 1; };
int main(){ shared_ptr<test> p1(new test); cout<<p1->a<<endl; cout<<(*p1).a<<endl; { shared_ptr<test> p2(p1); cout<<"p2:"<<p2.use_count()<<endl; shared_ptr<test> p3 = p2; cout<<"p2:"<<p2.use_count()<<endl; cout<<"p3:"<<p3.use_count()<<endl;
shared_ptr<test> p4(new test); cout<<"Before p4:"<<p4.use_count()<<endl; p4 = p3; cout<<"After p4:"<<p4.use_count()<<endl; } cout<<"p1:"<<p1.use_count()<<endl; }
|
unique_ptr的简单实现
主要包括一下成员函数:
构造函数;
移动构造函数;
析构函数;
禁用拷贝构造函数;
禁用拷贝赋值函数 operator=();
reset(): 释放源资源,指向新资源;
release(): 返回资源,放弃对资源的管理;
get(): 返回资源,只是公外部使用,依然管理资源;
operator bool(): 是否持有资源;
operator*();
operator->();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <iostream> #include <cassert>
template<typename T> class unique_ptr{ private: T* ptr; public: unique_ptr(T* _ptr):ptr(_ptr){}; unique_ptr(unique_ptr&& rhs):ptr(rhs.release()){}; ~unique_ptr(){ if(ptr) delete ptr; ptr = nullptr; } unique_ptr(const unique_ptr& rhs) = delete; unique_ptr& operator=(const unique_ptr& rhs) = delete; public: void reset(T* _ptr){ if(ptr) delete ptr; ptr = _ptr; } T* release(){ T* pTemp = ptr; ptr = nullptr; return pTemp; } T* get(){ return ptr; } public: explicit operator bool() const{ return ptr != nullptr; } T& operator*(){ assert(ptr); return *ptr; } T* operator->(){ assert(ptr); return ptr; } };
class test{ public: int a = 1; };
int main(){ unique_ptr<test> x(new test); cout<<"x->a:"<<(*x).a<<endl; unique_ptr<test> y = move(x); if(!x) cout<<"Moved x!!"<<endl; cout<<"y->a:"<<y->a<<endl; }
|
参考文献:
[1] https://zhuanlan.zhihu.com/p/344953368
[2] https://www.ccppcoding.com/archives/202