In modern C ++ style, there are two key concepts:
The property concerns the owner of an object / resource (in this case, a Device instance). Various std::unique_ptr , boost::scoped_ptr or std::shared_ptr belong to the property.
Nullity is much simpler: it simply expresses whether a given object can be null, and does not care about anything else, and, of course, not about ownership!
You were entitled to move the implementation of your class towards unique_ptr (in general), although you might need a smart pointer with deep copy semantics if your goal is to implement PIMPL.
This clearly indicates that your class is responsible for this chunk of memory and is neatly dealing with all the different ways that a memory leak might otherwise.
Most users of resources, on the other hand, did not care, however.
As long as the function does not save the link to the object (it stores it on the map or something else), then all that matters is that the lifetime of the object exceeds the duration of the function call.
Thus, the choice of the parameter transfer method depends on its possible Nullity:
- Never null? Pass the link
- Perhaps null? Pass a pointer, a simple empty pointer, or a pointer class (e.g. with a trap at level zero)
Matthieu M. Mar 14 2018-12-12T00: 00Z
source share