c++实现引用计数智能指针
2017-08-19 14:08
501 查看
主要的思路是使用一个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;
};
相关文章推荐
- C++引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现及改进
- 智能指针和引用计数以及String的C++实现
- 【转】C++ 引用计数技术及智能指针的简单实现
- 非常经典的C++ 引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- C++ 引用计数技术及智能指针的简单实现
- 【C++】智能指针之引用计数的实现
- C++ 引用计数技术及智能指针的简单实现
- [置顶]C++ 引用计数技术及智能指针的简单实现
- 引用计数的智能指针的实现与思考
- C++ — 智能指针的简单实现以及循环引用问题
- C++的引用计数j控制智能指针——>Java的引用计数管理共享对象
- 智能指针的实现--使用引用计数实现以及原理
- OSG中引用计数与智能指针实现原理
- 带引用计数的智能指针实现
- 关于智能指针,引用计数的学习与实现