Choice of smart pointer is a choice of ownership strategy. You have to ask yourself this question:
- Is
MainClass
the sole owner of theBuffer
instance? Or does it make sense for theBuffer
instance stored inMainClass
to outlive theMainClass
object? (Or would it make sense ifMainClass
became a component of a larger system and lost its application-lifetime status?)
If the answer is makes sense, go with a shared pointer. If the answer is sole owner/doesn't make sense, use something that expresses unique ownership - scoped_ptr
(or std::unique_ptr
, if available).
If you end up with unique ownership, use option 'a' for passing the object along. A function should only ever take a pointer parameter if the null pointer is a valid input for it.
If you end up with shared ownership, there are two possible situations. For passing the buffer to places which share the ownership, pass the shared_ptr
itself. For passing it to places which just observe/modify, use 'a' as above.