Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] calling functions by reference
From: Bo Persson (bop_at_[hidden])
Date: 2011-05-29 06:28:46


Sid Sacek wrote:
> -----Original Message-----
> From: boost-bounces_at_[hidden]
> [mailto:boost-bounces_at_[hidden]] On Behalf Of Matt Calabrese
> Sent: Saturday, May 28, 2011 10:53 PM
> To: boost_at_[hidden]
> Subject: Re: [boost] [shared_ptr] calling functions by reference
>
>> One common reason given to pass by value is to implicitly handle
>> the case where you call a separate function which may release your
>> shared pointer as a side effect. For instance:
>>
>> //////////
>> shared_ptr< int > a( new int( 2 ) );
>>
>> void foo() { a.reset(); }
>>
>> void bar( shared_ptr< int > const& arg )
>> {
>> foo(); *arg = 0; // Kaboom
>> }
>>
>> int main() { bar( a ); }
>>
>
> I agree with you that your example above will crash the program,
> but calling Reset() is micro-managing the smart-pointer. There
> should be no good reason to call reset on a regular basis, and only
> have objects go out of scope. That is my understanding of the point
> of the shared_ptr<>.
>

If you intend to pass by reference, why use a shared_ptr in the first
place?

Bo Persson


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