c++实现引用计数智能指针
来源:互联网 发布:网络棋牌开发公司 编辑:程序博客网 时间:2024/06/04 19:56
主要的思路是使用一个int* 的指针进行计数,在构造函数时候设置为0,并加1(或者直接设置为1)。然后赋值和复制构造时候把int* 和数据保存的指针T* mP传到另外一个类中。在赋值的时候要注意左边的指针是否已经有数据了,有数据就要先-1,然后再进行赋值。
template<typename T>
class ref1
{
public:
explicit ref1(T* p = NULL) : mP(p), sCount(new int)
{
*sCount = 0;
++(*sCount);
std::cout << "constructor" << std::endl;
std::cout << *sCount << std::endl;
}
ref1(const ref1& copy)
{
if (this != ©)
{
this->mP = copy.mP;
this->sCount = copy.sCount;
++(*sCount);
std::cout << "copy " << std::endl;
std::cout << *sCount << std::endl;
}
}
ref1& operator=(const ref1& rhs)
{
if (mP == rhs.mP)
{
return *this;
}
std::cout << "= construct2 " << std::endl;
std::cout << *sCount << std::endl;
//原来已经有一个对象了
if (sCount > 0)
{
std::cout << *sCount << std::endl;
--(*sCount);
std::cout << *sCount << std::endl;
if (*sCount == 0)
{
std::cout << "delete other" << std::endl;
if (mP != nullptr)
{
delete mP;
mP = nullptr;
}
if (sCount != nullptr)
{
delete sCount;
sCount = nullptr;
}
}
}
this->mP = rhs.mP;
this->sCount = rhs.sCount;
++(*sCount);
std::cout << "= construct " << std::endl;
std::cout << *sCount << std::endl;
return *this;
}
~ref1()
{
--(*sCount);
std::cout << "deconstruct " << std::endl;
std::cout << *sCount << std::endl;
if (*sCount == 0)
{
std::cout << "delete" << std::endl;
if (mP != nullptr)
{
delete mP;
mP = nullptr;
}
if (sCount != nullptr)
{
delete sCount;
sCount = nullptr;
}
}
}
protected:
int *sCount;
T* mP;
};
template<typename T>
class ref1
{
public:
explicit ref1(T* p = NULL) : mP(p), sCount(new int)
{
*sCount = 0;
++(*sCount);
std::cout << "constructor" << std::endl;
std::cout << *sCount << std::endl;
}
ref1(const ref1& copy)
{
if (this != ©)
{
this->mP = copy.mP;
this->sCount = copy.sCount;
++(*sCount);
std::cout << "copy " << std::endl;
std::cout << *sCount << std::endl;
}
}
ref1& operator=(const ref1& rhs)
{
if (mP == rhs.mP)
{
return *this;
}
std::cout << "= construct2 " << std::endl;
std::cout << *sCount << std::endl;
//原来已经有一个对象了
if (sCount > 0)
{
std::cout << *sCount << std::endl;
--(*sCount);
std::cout << *sCount << std::endl;
if (*sCount == 0)
{
std::cout << "delete other" << std::endl;
if (mP != nullptr)
{
delete mP;
mP = nullptr;
}
if (sCount != nullptr)
{
delete sCount;
sCount = nullptr;
}
}
}
this->mP = rhs.mP;
this->sCount = rhs.sCount;
++(*sCount);
std::cout << "= construct " << std::endl;
std::cout << *sCount << std::endl;
return *this;
}
~ref1()
{
--(*sCount);
std::cout << "deconstruct " << std::endl;
std::cout << *sCount << std::endl;
if (*sCount == 0)
{
std::cout << "delete" << std::endl;
if (mP != nullptr)
{
delete mP;
mP = nullptr;
}
if (sCount != nullptr)
{
delete sCount;
sCount = nullptr;
}
}
}
protected:
int *sCount;
T* mP;
};
阅读全文
0 0
- 【C++】智能指针之引用计数的实现
- 带引用计数的智能指针实现
- 基于引用计数的智能指针实现
- 引用计数板的智能指针实现
- shared_ptr基于引用计数智能指针实现
- shared_ptr基于引用计数智能指针实现
- shared_ptr基于引用计数智能指针实现
- c++实现引用计数智能指针
- 引用计数智能指针
- 智能指针的实现--使用引用计数实现以及原理
- 智能指针引用计数问题
- 智能指针(引用计数)
- C++引用计数智能指针
- 引用计数型智能指针
- 带引用计数智能指针
- 通过引用计数实现智能指针(C++ Primer)
- 一种基于引用计数机制的智能指针实现
- 引用计数的智能指针的实现与思考
- Nginx模块开发指南中文版
- @NotNull和@NotEmpty和@NotBlank区别
- Scala函数式编程(六)——闭包
- 一致性哈希算法
- AttributeError: module 'cv2' has no attribute 'SIFT'
- c++实现引用计数智能指针
- Qt笔记-MSVC报newline in constant(即奇数个中文汉字的问题)
- 2-1 Java动态加载类
- pip源-PyPI使用国内源
- 第一个docker测试容器(交互式)
- Win7+VS2013+OpenCV3.0.0详细配置
- 图像的卷积
- 指定CentOS7虚拟机IP
- linux-基础-进程通讯(一)-管道通信/信号/内存共享