📄 shared_ptr.h
字号:
//---------------------------------------------------------------------------
#ifndef shared_ptrH
#define shared_ptrH
//---------------------------------------------------------------------------
namespace DoxEngine
{
template <class T> class shared_ptr
{
public:
// Constructors
shared_ptr()
{
ptr = NULL;
count = new int;
*count = 1;
}
shared_ptr(const shared_ptr<T> &original)
{
ptr = original.ptr;
count = original.count;
(*count)++;
}
shared_ptr(T* original)
{
ptr = original;
count = new int;
*count = 1;
}
~shared_ptr()
{
Release();
}
//assignment
shared_ptr<T> &operator=(const shared_ptr &rhs)
{
if (this == &rhs)
return *this;
Release();
ptr = rhs.ptr;
count = rhs.count;
Acquire();
return *this;
}
shared_ptr<T> &operator=(const T* rhs)
{
if (ptr == rhs)
return *this;
Release();
ptr = rhs;
count = new int;
*count = 1;
}
//Use
T* operator->()
{
return ptr;
}
private:
T* ptr;
int* count;
void Acquire()
{
(*count)++;
}
void Release()
{
(*count)--;
if (!(*count))
{
delete count;
if (ptr)
delete ptr;
ptr = NULL;
}
}
// Comparison
bool operator>(const shared_ptr<T> &rhs)
{
return this>&rhs;
}
};
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -