📄 usercount.cpp
字号:
#include "usercount.h"
namespace mylib{
Usecount::Usecount():ref( new int(1) ){}
Usecount::Usecount(const Usecount& use)
{
++*ref;
}
bool Usecount::MakeOnly()//确保引用计数为一,写拷贝支持使用
{
if(*ref == 1)
return false;
--*ref;
ref=new int(1);
return true;
}
bool Usecount::ReAttach(const Usecount& u)
{
++*u.ref;//最后总要加一,先加后可以免做自赋值判断
if(--*ref ==0)
{ delete ref;
ref=u.ref;
return true;
}
ref=u.ref;
return false;
}
Usecount::~Usecount()
{
if(--*ref == 0)
{
delete ref;
}
ref=0;
}
}//end of mylib
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -