shared_ptr.h
来自「把html转成txt 把html转成txt」· C头文件 代码 · 共 105 行
H
105 行
//---------------------------------------------------------------------------
#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 + =
减小字号Ctrl + -
显示快捷键?