Boost logo

Boost Users :

From: Amerio (amerio_at_[hidden])
Date: 2005-01-21 03:26:53

Kosta wrote:
> Hi!
> Don't know, if this is a FAQ, but I couldn't find an answer to this
> question:
> Is there a common sense about how a shared_ptr<> should be passed to
> functions/methods: by-value or by-(const-)reference?
> 1) void test(boost::shared_ptr<A> a);
> 2) void test(boost::shared_ptr<A>& const a);

You should pass by value. Passing a shared_ptr means giving the opportunity
to the callee to use the data without worrying about its lifetime.
Would you pass it by ref, the callee won't own the the pointee and hence
cannot use it reliabily (it may get deleted behind its back).
By passing it by value, the callee makes a copy of the shared_ptr, hence
incrementing the ref count. The pointer won't get deleted while the callee
use the pointer.
Hope it makes sense.

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at