仿智能指针
来源:互联网 发布:淘宝客服话术模板 编辑:程序博客网 时间:2024/06/05 14:10
#pragma once
#ifndef _REF_PTR_h
#define _REF_PTR_h
class CRefObject
{
public:
CRefObject()
{
m_nRefCount = 0;
}
virtual ~CRefObject()
{
}
int GetRefCount() const
{
return m_nRefCount;
}
int AddRefCount()
{
return ++m_nRefCount;
}
int SubRefCount()
{
return --m_nRefCount;
}
void ResetRefCount()
{
m_nRefCount = 0;
}
private:
int m_nRefCount;
};
//T 必须继承自 CRefObject
template<typename T>
class CRefPtr
{
public:
T* operator->() const
{
return m_ptrObj;
}
T& operator()() const
{
assert(m_ptrObj != NULL);
return *m_ptrObj;
}
T& operator*() const
{
assert(m_ptrObj != NULL);
return *m_ptrObj;
}
T* GetPtr() const
{
return m_ptrObj;
}
bool IsNull() const
{
return m_ptrObj == NULL;
}
explicit CRefPtr(T* p = NULL)
{
m_ptrObj = p;
if(p != NULL)
{
p->AddRefCount();
}
}
CRefPtr(const CRefPtr& ref)
{
m_ptrObj = ref.m_ptrObj;
if(m_ptrObj != NULL)
{
m_ptrObj->AddRefCount();
}
}
~CRefPtr()
{
try
{
if(m_ptrObj != NULL && m_ptrObj->SubRefCount() == 0)
{
delete m_ptrObj;
}
}
catch (...)
{
}
}
CRefPtr& operator = (const CRefPtr& ref)
{
if(this != &ref)
{
if(m_ptrObj != NULL
&& m_ptrObj->SubRefCount() == 0)
{
delete m_ptrObj;
}
//lint -e{1555}
m_ptrObj = ref.m_ptrObj;
//lint +e1555
if(m_ptrObj != NULL)
{
m_ptrObj->AddRefCount();
}
}
return *this;
}
bool operator == (const CRefPtr& ref) const
{
return m_ptrObj == ref.m_ptrObj;
}
private:
T* m_ptrObj;
};
#ifndef _REF_PTR_h
#define _REF_PTR_h
class CRefObject
{
public:
CRefObject()
{
m_nRefCount = 0;
}
virtual ~CRefObject()
{
}
int GetRefCount() const
{
return m_nRefCount;
}
int AddRefCount()
{
return ++m_nRefCount;
}
int SubRefCount()
{
return --m_nRefCount;
}
void ResetRefCount()
{
m_nRefCount = 0;
}
private:
int m_nRefCount;
};
//T 必须继承自 CRefObject
template<typename T>
class CRefPtr
{
public:
T* operator->() const
{
return m_ptrObj;
}
T& operator()() const
{
assert(m_ptrObj != NULL);
return *m_ptrObj;
}
T& operator*() const
{
assert(m_ptrObj != NULL);
return *m_ptrObj;
}
T* GetPtr() const
{
return m_ptrObj;
}
bool IsNull() const
{
return m_ptrObj == NULL;
}
explicit CRefPtr(T* p = NULL)
{
m_ptrObj = p;
if(p != NULL)
{
p->AddRefCount();
}
}
CRefPtr(const CRefPtr& ref)
{
m_ptrObj = ref.m_ptrObj;
if(m_ptrObj != NULL)
{
m_ptrObj->AddRefCount();
}
}
~CRefPtr()
{
try
{
if(m_ptrObj != NULL && m_ptrObj->SubRefCount() == 0)
{
delete m_ptrObj;
}
}
catch (...)
{
}
}
CRefPtr& operator = (const CRefPtr& ref)
{
if(this != &ref)
{
if(m_ptrObj != NULL
&& m_ptrObj->SubRefCount() == 0)
{
delete m_ptrObj;
}
//lint -e{1555}
m_ptrObj = ref.m_ptrObj;
//lint +e1555
if(m_ptrObj != NULL)
{
m_ptrObj->AddRefCount();
}
}
return *this;
}
bool operator == (const CRefPtr& ref) const
{
return m_ptrObj == ref.m_ptrObj;
}
private:
T* m_ptrObj;
};
#endif
example:
class testobj public CRefObject
{
}
CRefPtr<testobj > m_test;
m_test= CRefPtr<testobj > (new testobj () );
2 0
- 仿智能指针
- 【C++】智能指针shared_ptr 定位删除器(仿函数)
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 智能指针
- 深入浅出nodejs学习笔记-前三章 简介、模块机制、异步I/O
- Swift分支语句—switch语句
- python使用代理访问网站
- QT中多个按钮响应同一个槽函数
- leetcode-71 simplify path
- 仿智能指针
- 游戏制作要处理的相关细节
- 什么是P问题、NP问题和NPC问题
- 【Spring】使用Spring发送邮件
- AngularJS路由 $state服务、路由事件、获取路由参数
- ZOJ2208-To and Fro
- TextView 加边框
- 彻底理解python中函数内赋值操作和对象的可变性
- 树形DP