Different pointer types cater for different ownership semantics:
unique_ptr
s are for unique ownership, this is why your current code does not workshared_ptr
s are for shared ownership - when the lastshared_ptr
managing an object goes out of scope, it is destroyed- raw pointers (
*
) don't really say anything about the semantics they are modeling, but you have to enforce all constraints manually - that's what makes them dangerous. They are however, safe, (and in my opinion the sensible choice) for pointers that do never have ownership of the object pointed to.
So: Your statesToBeAdded
is safe as long as you can guarantee that you never have anything in there that isn't already in states
. statesToBeRemoved
is different: The objects are already there and in states
, and it would make sense to have states
take ownership of the objects - hence raw pointers would be a fair choice for statesToBeRemoved
.
Note that all this is based on the few lines of code you have shown, depending on the circumstances it may well be that it makes sense to store State
s instead of unique_ptr<State>
s in your states
, etc ...