Jump to content
  • Advertisement
Sign in to follow this  
daniel_i_l

When to use boost smart pointers

This topic is 3861 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
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.

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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!