usercount.cpp

来自「经常用到的 socketAPI」· C++ 代码 · 共 54 行

CPP
54
字号
#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 + =
减小字号Ctrl + -
显示快捷键?