Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does adding a reference to a shared ptr increase the reference count

Tags:

c++

shared-ptr

Suppose I have a method as such

void foo(const boost::shared_ptr<Pfoo>& rx)
{
   myvector->push_back(rx);
}

I read that when a boost::shared_ptr is passed as a reference its reference count does not increase. Now what would happen in the above scenario if the actual ptr ran out of scope ?

like image 996
Casper_2211 Avatar asked Apr 12 '13 09:04

Casper_2211


1 Answers

When pass pointer by reference - no copy occured, no reference-counter incrementing.

myvector->push_back(rx);

Here, in vector will be pushed copy of rx, not rx, so, reference-counter will be incremented.

like image 181
ForEveR Avatar answered Nov 15 '22 08:11

ForEveR