实现一个share_ptr

·```

include

include

include

using namespace std;

template
class Shared_Ptr{
public:
Shared_Ptr(T* ptr = nullptr)
:_pPtr(ptr)
, _pRefCount(new int(1))
, _pMutex(new mutex)
{}
~Shared_Ptr()
{
Release();
}
Shared_Ptr(const Shared_Ptr& sp)
:_pPtr(sp._pPtr)
, _pRefCount(sp._pRefCount)
, _pMutex(sp._pMutex)
{
AddRefCount();
}
Shared_Ptr& operator=(const Shared_Ptr& sp)
{
//if (this != &sp)
if (_pPtr != sp._pPtr)
{
// 释放管理的旧资源
Release();
// 共享管理新对象的资源,并增加引用计数
_pPtr = sp._pPtr;
_pRefCount = sp._pRefCount;
_pMutex = sp._pMutex;
AddRefCount();
}
return this;
}
T& operator
(){
return _pPtr;
}
T
operator->(){
return _pPtr;
}
int UseCount() { return _pRefCount; }
T
Get() { return _pPtr; }
void AddRefCount()
{
_pMutex->lock();
++(_pRefCount);
_pMutex->unlock();
}
private:
void Release()
{
bool deleteflag = false;
_pMutex->lock();
if (--(
_pRefCount) == 0)
{
delete _pRefCount;
delete _pPtr;
deleteflag = true;
}
_pMutex->unlock();
if (deleteflag == true)
delete _pMutex;
}
private:
int _pRefCount;
T
_pPtr;
mutex* _pMutex;
};

实现一个share_ptr

上一篇:输入框为空时,按钮灰色不可点


下一篇:阿里云的专业技术认证分为哪些?为啥要获得阿里云的ACP认证!