Search code examples
c++shared-ptr

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


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 ?


Solution

  • 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.