CRefCount
发布日期:2021-06-30 22:07:54
浏览次数:2
分类:技术文章
本文共 4544 字,大约阅读时间需要 15 分钟。
一个单线程的引用计数类, 针对具体类.
/// @file exam_x_x.cpp/// @brief 测试引用计数类#include#include #include "RefCount.h"#include "RefOwner.h"using namespace std;void clear_cin();void fnTestRefCount();int main(int argc, char** argv, char** envp){ fnTestRefCount(); cout << "END, press any key to quit" << endl; clear_cin(); getchar(); return 0;}void fnTestRefCount(){ cout << endl << "fnTestRefCount() ==> CRefCount ref1(new CRefOwner(\"ref1\"));" << endl << endl; CRefCount ref1(new CRefOwner("ref1")); cout << endl << "fnTestRefCount() ==> CRefCount ref2(ref1);" << endl << endl; CRefCount ref2(ref1); cout << endl << "fnTestRefCount() ==> CRefCount ref3(new CRefOwner(\"ref3\"));" << endl << endl; CRefCount ref3(new CRefOwner("ref3")); cout << endl << "fnTestRefCount() ==> CRefCount ref4(ref3);" << endl << endl; CRefCount ref4(ref3); cout << endl << "fnTestRefCount() <==" << endl << endl;}void clear_cin(){ cin.clear(); cin.sync();}
// RefCount.h: interface for the CRefCount class.////#if !defined(AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_)#define AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include "RefOwner.h"class CRefCount {public: CRefCount(CRefOwner* pOwner); CRefCount(const CRefCount* pObj); CRefCount(const CRefCount& Obj); virtual ~CRefCount(); void addRef(); ///< 引用计数++ int releaseRef(); ///< 引用计数--private: void cpyAndAddRef(const CRefCount* pObj);private: int* m_piRefCount; ///< 引用计数点数指针 CRefOwner* m_pOwner; ///< 引用计数所有者指针s};#endif // !defined(AFX_REFCOUNT_H__2F7675D6_BDC0_4BE6_A0FD_14F51476D8ED__INCLUDED_)
// RefCount.cpp: implementation of the CRefCount class.////#include#include #include #include "RefCount.h"//// Construction/Destruction//using namespace std;CRefCount::CRefCount(CRefOwner* pOwner):m_piRefCount(NULL),m_pOwner(NULL){ cout << "CRefCount::CRefCount this = " << this << " pOwner = 0x" << pOwner << endl; m_piRefCount = new int(0); m_pOwner = pOwner; addRef();}CRefCount::CRefCount(const CRefCount* pObj){ cpyAndAddRef(pObj);}CRefCount::CRefCount(const CRefCount& Obj){ cpyAndAddRef(&Obj);}void CRefCount::cpyAndAddRef(const CRefCount* pObj){ if ((NULL == pObj) || (this == pObj)) { return; } m_piRefCount = pObj->m_piRefCount; m_pOwner = pObj->m_pOwner; addRef();}CRefCount::~CRefCount(){ if (0 == releaseRef()) { cout << "CRefCount::~CRefCount() this = " << this << " 0 == releaseRef()" << endl; if (NULL != m_pOwner) { cout << "CRefCount::~CRefCount() this = " << this << " delete m_pOwner " << m_pOwner << endl; delete m_pOwner; m_pOwner = NULL; } if (NULL != m_piRefCount) { cout << "CRefCount::~CRefCount() this = " << this << " delete m_piRefCount " << m_piRefCount << endl; delete m_piRefCount; m_piRefCount = NULL; } }}void CRefCount::addRef(){ int iRc = 0; if (NULL != m_piRefCount) { (*m_piRefCount)++; iRc = *m_piRefCount; cout << "CRefCount::addRef this = " << this << " *m_piRefCount = " << *m_piRefCount << endl; }}int CRefCount::releaseRef(){ int iRc = 0; iRc = (NULL != *m_piRefCount) ? (--(*m_piRefCount)) : 0; cout << "CRefCount::releaseRef this = " << this << " *m_piRefCount = " << iRc << endl; return iRc;}
// RefOwner.h: interface for the CRefOwner class.////#if !defined(AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_)#define AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class CRefOwner {public: CRefOwner(const char* pszName); virtual ~CRefOwner();private: char *m_pszName;};#endif // !defined(AFX_REFOWNER_H__C08C0436_4EE1_4AE6_9EC2_B6E320280FAE__INCLUDED_)
// RefOwner.cpp: implementation of the CRefOwner class.////#include#include #include "RefOwner.h"using namespace std;//// Construction/Destruction//CRefOwner::CRefOwner(const char* pszName){ cout << "CRefOwner::CRefOwner this = " << this << " pszName = " << pszName << " " << endl; m_pszName = new char[strlen(pszName) + 1]; strcpy(m_pszName, pszName);}CRefOwner::~CRefOwner(){ if (NULL != m_pszName) { cout << "CRefOwner::~CRefOwner this = " << this << " m_pszName = " << m_pszName << endl; delete[] m_pszName; m_pszName = NULL; }}
转载地址:https://lostspeed.blog.csdn.net/article/details/50339927 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月22日 02时14分01秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
数据结构之线性链表
2019-04-30
JQuery使用validate插件完成校验
2019-04-30
关于java的继承
2019-04-30
关于java的内部类
2019-04-30
关于java的枚举
2019-04-30
一个简单的layui登陆界面
2019-04-30
SQL考试常见题目
2019-04-30
使用Spring Boot写一个简单的Hello World
2019-04-30
Spring Boot整合Servlet使用
2019-04-30
SpringBoot 文件上传
2019-04-30
我居然在Github上找到了一个完整的停车系统(附源码地址)
2019-04-30
大厂经典面试题:Redis为什么这么快?
2019-04-30
精通Spring?请吃我一狗腿!
2019-04-30
培训班老师说可以用这个干掉一大批面试者
2019-04-30
阿里四面,居然栽在一道排序算法上
2019-04-30
【Java编码规范】《阿里巴巴Java开发手册(正式版)》发布!
2019-04-30
如何在二三线城市月薪过万(一)看完这篇后端简历优化,包你面试不断
2019-04-30
阿里P8大神教你十分钟构建好SpringBoot + SSM框架 成功晋升
2019-04-30
Linux运维-搭建高可用Redis缓存
2019-04-30