从上到下依次是auto_ptr unique_ptr 以及shared_ptr实现以及简单的调用。用作记录
//auto_ptr.h #ifndef _AUTOPTR_ #define _ATUTOPTR #include#include template class Smart_auto { public: explicit Smart_auto(T* _data = nullptr) :data(_data) { } Smart_auto(const Smart_auto& a) { data = a.data; a.data = nullptr; } Smart_auto& operator= (const Smart_auto &a) { if (&a !=this) { this->data = a.data; a.data = nullptr; } return *this; } ~Smart_auto() { delete data; } T* get() { return data; } //释放指针所有权 T* release() { T* temp = data; data = nullptr; return temp; } void reset() { delete data; data = nullptr; } T& operator* () { return *data; } T* operator-> () { return data; } private: T* data; }; #endif // ! _AUTOPTR_
//unique_ptr.h #ifndef _unique #define _unique #include#include template class Smart_unique { public: explicit Smart_unique(T* _ptr = nullptr) :ptr(_ptr) {} Smart_unique(const Smart_unique&& temp) { ptr = temp.ptr; } Smart_unique& operator= (Smart_unique&& temp) { if (temp == *this) return *this; delete ptr; ptr = temp.ptr; temp.ptr = nullptr; return *this; } ~Smart_unique() { delete ptr; } T operator* () { return *ptr; } T* get() { return ptr; } T* release() { T* temp = ptr; ptr = nullptr; return temp; } void reset() { delete ptr; ptr = nullptr; } private: T* ptr; }; #endif
shared_ptr.h
#ifndef _SharedPtr #define _SharedPtr #include#include #include
调用
#include"auto_ptr.h" #include"unique_ptr.h" #include"Shared_ptr.h" #includeusing namespace std; void test_01() { int* data = new int(5); Smart_auto my_auto(data); cout << "test " << endl; cout << *my_auto << endl; my_auto.reset(); //cout << my_auto.release() << endl; cout << "地址 " << data << endl; cout << *data << endl; } void test_02() { int* data = new int(5); Smart_unique my_auto(data); //Smart_unique my_auto1(data); error 禁止两个指针指向同一片数据 独享资源 Smart_unique my_auto1(new int(100)); //Smart_unique my_auto2(my_auto1); error禁止使用拷贝构造函数。 cout << "test " << endl; cout << *my_auto << endl; my_auto.reset(); //cout << my_auto.release() << endl; cout << "地址 " << data << endl; cout << *data << endl; } void orin_test() { int* data = new int(5); shared_ptr pointer(data); cout << "用一个int 对象初始化 应该为 1 " << pointer.use_count() << endl; shared_ptr pointer1= shared_ptr (new int (9)); cout << "用一个临时对象初始化,应该为 1 " << pointer1.use_count() << endl; shared_ptr pointer2(pointer); cout << "用一个已知对象初始化,应该为 2 " << pointer2.use_count() << endl; shared_ptr pointer3 = pointer1; cout << "用一个已知对象构造,应该为 2 " << pointer3.use_count() << endl; pointer3 = pointer; cout << "更改指向 分别应该为 3 ,1 " << pointer3.use_count()<<" " < pointer4(data1); pointer4.reset(); cout << "usecount " << pointer4.use_count() << endl; cout << "int data 数据 " << *data1 << endl; } void my_test() { cout << "我自己的测试" << endl; cout << "*******************" << endl; cout << "*******************" << endl; cout << "*******************" << endl; cout << "*******************" << endl; int* data = new int(5); Share_ptr pointer(data); cout << "用一个int 对象初始化 应该为 1 " << pointer.use_count() << endl; cout << "*pointer " << *pointer << endl; Share_ptr pointer1 = Share_ptr (new int(9)); cout << "用一个临时对象初始化,应该为 1 " << pointer1.use_count() << endl; Share_ptr pointer2(pointer); cout << "用一个已知对象初始化,应该为 2 " << pointer2.use_count() << endl; Share_ptr pointer3 = pointer1; cout << "用一个已知对象构造,应该为 2 " << pointer3.use_count() << endl; pointer3 = pointer; cout << "更改指向 分别应该为 3 ,1 " << pointer3.use_count() << " " << pointer1.use_count() << endl; int* data1 = new int(5); Share_ptr pointer4(data1); pointer4.reset(); cout << "usecount " << pointer4.use_count() << endl; cout << "int data 数据 " << *data1 << endl; } int main() { orin_test(); my_test(); }



