
Sid Sacek wrote:
-----Original Message----- From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] On Behalf Of Matt Calabrese Sent: Saturday, May 28, 2011 10:53 PM To: boost@lists.boost.org 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