June 2025
Intermediate to advanced
1093 pages
33h 24m
English
You can also put all forms of pointers into standard containers. However, the same rules apply as for pointers outside of containers; that is, ownership semantics are determined by the type of pointer you choose, not by the type of container:
vector<Planet*> planets only holds the pointers themselves and does not feel responsible for the heap objects without your intervention. If planets is automatically removed as a stack object, delete is not automatically called for the elements. You must either do this yourself beforehand if planets is to be the owner of the objects, or keep the addresses in another variable that is then the owner.
vector<unique_ptr<Planet>> is always automatically the owner of the elements, ...
Read now
Unlock full access