Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr vs pass object by reference
From: Michael Jackson (mike.jackson_at_[hidden])
Date: 2010-07-28 10:58:33


On 7/28/10 9:33 AM, in article
9FA45EF1AB90804BB8FF5A4A8AD0B97305A895A801_at_IE2RD2XVS031.red002.local,
"Patrick Loney" wrote:

>
>> If I can pass the object by reference to a function, then is there a
>> need to use shared pointers?
>
> Not if you know the lifetime of your object and it won't be deleted before the
> function has finished. You may find scoped pointers useful though:
> http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm

I use shared_ptr like a "poor-man's" garbage collection. Instead of having
to remember to "delete" or "free" my pointers I can wrap them in a
shared_ptr and know that when the last reference to the pointer goes out of
scope the pointer will be cleaned up. In fact it is better if you pass a
shared pointer by value otherwise the reference count will NOT be correct
which may or may not have bad side effects. Maybe I am using shared_ptr
wrong but at least for my project it seems to work well. *I did come from a
Java Background before C++ so this type of programming model is natural to
me for better or worse.

Mike Jackson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net