Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] calling functions by reference
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2011-05-29 12:17:06


On Sun, May 29, 2011 at 6:28 AM, Bo Persson <bop_at_[hidden]> 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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk