Does the link add to the general ptr link counter

Suppose I have a method like this

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

I read that when boost :: shared_ptr is passed as a link, its link count does not increase. Now, what happens in the scenario described above if the actual ptr has run out of scope?

+5
source share
5 answers

When a pointer is passed by reference - there was no copy, the reference counter does not increase.

myvector->push_back(rx);

Here rx, a copy will be pressed in the vector , not rx, therefore, the reference count will be increased.

+5
source

, - . , rx . push_back, rx .

+2

. :

void foo(const boost::shared_ptr<Pfoo>& rx)

, rx :

myvector->push_back(rx);
0

smart_ptr , . . , .

myvector->push_back(rx); //Copy of object is passed.
0

. , ( ++) (). () . () , .

This happened to me once and caused a serious mistake that was difficult to track and had real consequences. Therefore, do not pass references to objects unless you are sure that the function may have the effect of destroying the reference object!

push_back creates a new link to the object.

0
source

All Articles