That depends on what you want. Should the callee share ownership of the object? Then it needs its own copy of the shared_ptr
. So pass it by value.
If a function simply needs to access an object owned by the caller, go ahead and pass by (const) reference, to avoid the overhead of copying the shared_ptr
.
The best practice in C++ is always to have clearly defined ownership semantics for your objects. There is no universal "always do this" to replace actual thought.
If you always pass shared pointers by value, it gets costly (because they're a lot more expensive to copy than a raw pointer). If you never do it, then there's no point in using a shared pointer in the first place.
Copy the shared pointer when a new function or object needs to share ownership of the pointee.