
On 7/28/10 9:33 AM, in article 9FA45EF1AB90804BB8FF5A4A8AD0B97305A895A801@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