vector<unique_ptr<Car>> all_cars_;
sounds ideal for your application. Whoever owns all_cars
, then also owns all of the Car*
s in all_cars
. And this will be superior to:
vector<Car*> all_cars_;
for at least 2 reasons:
- you won't have to worry about the memory ownership code any longer.
- The compiler will keep you from accidentally having more than one entity own any of the pointers in
all_cars_
.
For:
vector<Car*> my_cars_;
it sounds like these are non-owning pointers. And so keeping them as Car*
is perfectly reasonable. This also allows you to freely make copies of any of these pointers. The only thing you'll need to watch out for is the invalidation of the pointers due to changes in all_cars_
, which you are surely already managing with your current design.