When to use boost smart pointers

Started by
5 comments, last by the_edd 16 years, 1 month ago
When is it better to use boost smart pointers that regular pointers? Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
All the time, apart from when you shouldn't.

Or to be slightly more helpful; I'm as yet to find a time when I want to use a raw pointer for practically anything... heck, last time I played with a C IO FILE pointer I made it into a boost smart pointer (shared_ptr I think, although I probably should have used scoped) and set the destruction function to the fclose() function for automagic clean up [smile]
Quote:Original post by phantom
(shared_ptr I think, although I probably should have used scoped)

scoped_ptr doesn't allow you to customize the destruction function. shared_ptr would have been the most convenient.
I agree, all the time!

What about when you are, say, creating a vector of pointers? Would you use a vector of shared_ptr's instead?

I was under the impression that smart pointers had a bit more setup required than regular pointers, and so might not be suitable for all replacements, but I'd love to be proven wrong.
Quote:Original post by Nairou
What about when you are, say, creating a vector of pointers? Would you use a vector of shared_ptr's instead?
Depending on your usage patterns you may be happier with ptr_vector. I've definitely had a vector of smart pointers, though.

Quote:I was under the impression that smart pointers had a bit more setup required than regular pointers, and so might not be suitable for all replacements, but I'd love to be proven wrong.
They do, but it's not usually significant. Write first. Profile and optimize later. Smart pointers are generally fairly easy to remove from your code where necessary.
If you find that you're having to write 'delete', then chances are you probably should have used a smart pointer.

This topic is closed to new replies.

Advertisement