从上到下依次是auto_ptr unique_ptr 以及shared_ptr实现以及简单的调用。用作记录
#ifndef _AUTOPTR_
#define _ATUTOPTR
#include <iostream>
#include <string>
template<typename T>
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
#ifndef _unique
#define _unique
#include <iostream>
#include <string>
template <typename T>
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<iostream>
#include<string>
#include<map>
#include<unordered_map>
using namespace std;
template<typename T>
class Share_ptr
{
private:
T* ptr;
static unordered_map<T*, int>hash;
public:
explicit Share_ptr(T* _ptr ):ptr(_ptr)
{
hash[_ptr] = 1;
}
Share_ptr(const Share_ptr& a)
{
ptr = a.ptr;
hash[ptr]++;
}
Share_ptr& operator= (const Share_ptr& a)
{
if (ptr)
{
hash[ptr]--;
if (hash[ptr] == 0)
{
delete ptr;
ptr = nullptr;
}
}
ptr = a.ptr;
hash[ptr]++;
return *this;
}
~Share_ptr()
{
if (hash[ptr] == 1)
{
delete ptr;
ptr = nullptr;
}
}
int use_count()
{
return hash[ptr];
}
T& operator*()
{
return *ptr;
}
T* operator ->()
{
return ptr;
}
bool unique()
{
return hash[ptr] == 1 ? true : false;
}
void reset()
{
hash[ptr]--;
if (hash[ptr] == 0)
{
delete ptr;
ptr = nullptr;
}
}
};
template <typename T>
unordered_map<T*, int>Share_ptr<T>::hash = unordered_map<T*, int>();
#endif
调用
#include"auto_ptr.h"
#include"unique_ptr.h"
#include"Shared_ptr.h"
#include<memory>
using namespace std;
void test_01()
{
int* data = new int(5);
Smart_auto<int> my_auto(data);
cout << "test " << endl;
cout << *my_auto << endl;
my_auto.reset();
cout << "地址 " << data << endl;
cout << *data << endl;
}
void test_02()
{
int* data = new int(5);
Smart_unique<int> my_auto(data);
Smart_unique<int> my_auto1(new int(100));
cout << "test " << endl;
cout << *my_auto << endl;
my_auto.reset();
cout << "地址 " << data << endl;
cout << *data << endl;
}
void orin_test()
{
int* data = new int(5);
shared_ptr<int>pointer(data);
cout << "用一个int 对象初始化 应该为 1 " << pointer.use_count() << endl;
shared_ptr<int>pointer1= shared_ptr<int>(new int (9));
cout << "用一个临时对象初始化,应该为 1 " << pointer1.use_count() << endl;
shared_ptr<int>pointer2(pointer);
cout << "用一个已知对象初始化,应该为 2 " << pointer2.use_count() << endl;
shared_ptr<int>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);
shared_ptr<int>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<int>pointer(data);
cout << "用一个int 对象初始化 应该为 1 " << pointer.use_count() << endl;
cout << "*pointer " << *pointer << endl;
Share_ptr<int>pointer1 = Share_ptr<int>(new int(9));
cout << "用一个临时对象初始化,应该为 1 " << pointer1.use_count() << endl;
Share_ptr<int>pointer2(pointer);
cout << "用一个已知对象初始化,应该为 2 " << pointer2.use_count() << endl;
Share_ptr<int>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<int>pointer4(data1);
pointer4.reset();
cout << "usecount " << pointer4.use_count() << endl;
cout << "int data 数据 " << *data1 << endl;
}
int main()
{
orin_test();
my_test();
}
|