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
"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:

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, kalb at, bjorn.karlsson at, gregod at, wekempf at