• Advertisement
Sign in to follow this  

When to use boost smart pointers

This topic is 3626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
If you find that you're having to write 'delete', then chances are you probably should have used a smart pointer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement