Are there any differences between the two pieces of code below? Is any of them preferable to the other?
operator=
boost::shared_ptr&
Assignment operator create a new shared object from existing one, incrementing the reference count
CSharedObj& CSharedObj::operator=(CSharedObj& r) noexcept
{
if(*this != r){
//detach from the previous ownership
if(0 == dec()) delete m_pControlObj;
//attach to the new control object and increment the reference count
r.inc();
m_pControlObj = r.m_pControlObj;
}
return *this;
}
while the reset call doesn't create the new shared object, but rather a new ownership - attaching to the new underlying pointee ( via control object)
void CSharedObj::reset(Ptr pointee) noexcept
{
//check if this is a last reference-detach from the previous ownership
if(0==dec()) delete m_pControlObj;
// create the ownership over the new pointee (refCnt = 1)
m_pControlObj = new (std::nothrow) CControlObj(pointee);
}