When to use boost smart pointers
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]
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.
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.
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 NairouDepending on your usage patterns you may be happier with ptr_vector. I've definitely had a vector of smart pointers, though.
What about when you are, say, creating a vector of pointers? Would you use a vector of shared_ptr's instead?
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
Popular Topics
Advertisement