C++重写SharedArrayPtr智能指针类
基于C++11中shared_ptr源码改写。代码都已经过测试检验。
头文件:kSharedArrayPtr.h
#ifndef _KSHAREDARRAYPTR_H_
#define _KSHAREDARRAYPTR_H_
#include <iostream>
//共享计数的管理在堆上分配数组的智能指针KSharedArrayPtr
template <typename T>
class KSharedArrayPtr
{
private:
T* m_ptr;
size_t* m_count;
size_t m_size;
public:
explicit KSharedArrayPtr(T* p = nullptr, size_t size = 0); //默认构造函数
KSharedArrayPtr(KSharedArrayPtr const& r); //复制构造函数
KSharedArrayPtr& operator=(KSharedArrayPtr const& r); //重载赋值运算符
~KSharedArrayPtr(); //析构函数
bool operator==(KSharedArrayPtr const& r); //重载运算符==
bool operator!=(KSharedArrayPtr const& r); //重载运算符!=
T* get()const; //返回当前存储的指针
void reset(T* p = nullptr, size_t size = 0); //重置当前存储的指针
int getCount(); //获取引用数
T& operator[](size_t i) const; //重载[]运算符
};
//默认构造函数
template <typename T>
KSharedArrayPtr<T>::KSharedArrayPtr(T* p, size_t size )
{
this->m_ptr = p;
this->m_size = size;
try
{
this->m_count = new size_t(1);
}
catch (...)
{
delete[] this->m_ptr; //申请失败,释放真实指针和引用计数的内存
this->m_ptr = nullptr;
delete this->m_count;
this->m_count = nullptr;
}
}
//复制构造函数
template <typename T>
KSharedArrayPtr<T>::KSharedArrayPtr(KSharedArrayPtr const& r)
{
this->m_count = r.m_count;
this->m_ptr = r.m_ptr;
this->m_size = r.m_size;
++(*this->m_count);
}
//重载赋值运算符
template <typename T>
KSharedArrayPtr<T>& KSharedArrayPtr<T>::operator=(KSharedArrayPtr const& r)
{
this->m_size = r.m_size;
this->m_count = r.m_count;
this->m_ptr = r.m_ptr;
++(*this->m_count);
return *this;
}
//析构函数
template <typename T>
KSharedArrayPtr<T>::~KSharedArrayPtr()
{
if (--(*this->m_count) == 0)
{
this->m_size = 0;
delete[]this->m_ptr;
this->m_ptr = nullptr;
delete this->m_count;
this->m_count = nullptr;
}
}
//重载比较运算符==
template <typename T>
bool KSharedArrayPtr<T>::operator==(KSharedArrayPtr const& r)
{
if (this->m_ptr == r.m_ptr)
return true;
else
return false;
}
//重载比较运算符!=
template <typename T>
bool KSharedArrayPtr<T>::operator!=(KSharedArrayPtr const& r)
{
if (this->m_ptr != r.m_ptr)
return true;
else
return false;
}
//返回当前存储的指针
template <typename T>
T* KSharedArrayPtr<T>::get() const
{
return m_ptr;
}
//重置当前存储的指针
template <typename T>
void KSharedArrayPtr<T>::reset(T* p,size_t size)
{
this->m_size = size;
if (--(*this->m_count) == 0)
{
delete[]this->m_ptr;
this->m_ptr = p;
delete this->m_count;
this->m_count = new size_t(1);
}
else
{
this->m_ptr = p;
this->m_count = new size_t(1);
}
}
//获取引用数
template <typename T>
int KSharedArrayPtr<T>::getCount()
{
return *this->m_count;
}
//重载[]运算符
template <typename T>
T& KSharedArrayPtr<T>::operator[](size_t i) const
{
return this->m_ptr[i];
}
#endif // _KSHAREDARRAYPTR_H_
测试文件:demo.cpp
#include "kSharedArrayPtr.h"
using namespace std;
void test1() //测试构造函数
{
int* a = new int[10];
int* b = new int[2]{ 100,99 };
KSharedArrayPtr<int> p1(a, 10);
KSharedArrayPtr<int> p2(p1);
KSharedArrayPtr<int> p3 = p2;
for (int i=0;i<10;i++)
a[i] = 2 * i;
p2.reset(b, 2);
for (int i = 0; i < 10; i++)
cout << p3[i] << " ";
cout << endl << p3.getCount() << endl;
if (p3==p2)
{
cout << 1 << endl;
}
if (p3 == p1)
cout << 2 << endl;
cout << *(p3.get()+2) << endl;
}
int main()
{
test1();
system("pause");
}