当前位置:   article > 正文

C++重写shared_ptr智能指针类_重写智能指针get方法

重写智能指针get方法

基于C++11中shared_ptr源码改写。全部代码都已经过测试检验。

头文件:kSharedPtr.h

#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_

  • 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
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167

测试文件:demo.cpp

#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");
}


  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/206262
推荐阅读
相关标签
  

闽ICP备14008679号