
On Sun, May 29, 2011 at 6:28 AM, Bo Persson <bop@gmb.dk> wrote:
If you intend to pass by reference, why use a shared_ptr in the first place?
Passing the shared pointer by reference doesn't change any of the reasons for why you'd use one to begin with. As always, you use shared pointers to have shared ownership of the pointee. Passing the shared pointer by reference or passing the pointee by reference directly just means that you do not (at least not immediately) want to create a new smart pointer that can extend the lifetime of the pointee. You still may want a reference to a shared pointer for several reasons, such as for internally storing a copy of the smart pointer in an object, creating a weak_ptr from it, etc. You don't want to needlessly copy smart pointers just as you don't want to needlessly copy any other object in C++. Working with shared pointers does not change any of the rationale for why one would generally pass an object by reference in C++. -- -Matt Calabrese