So , I have done a fair bit of googling trying to figure out if there is some best smart pointer to handle this but I have not been able to settle on a good choice.
struct Car
{
int m_doors;
int m_headlights;
//etc..
}
Lets say I have some class Car. I also have a std::map<int carkey, Car*>
that is the JunkYard.
A Car* can be added to the JunkYard by some Driver, the Driver itself new'd the Car, but the driver immediately loses all responsibility of the Car*.
I would like the JunkYard to be responsible for all its cars, Cars in the JunkYard can be accessed by people to remove say parts and take scrap metal from the Car , or can be crushed but the JunkYard destroying it forever.
At the end of the day the JunkYard destroys all of its Cars and shuts down.
--
A) If this were a multithreaded app where Drivers could access cars concurrently while the JunkYard could destroy cars I was thinking that probably a shared_ptr would be best? Is this overkill though? Other threads may access the car but they will never take long term ownership of it, it must always remain belonging to the JunkYard.
B) If this were single threaded what would be the best choice -assuming its still a map< some_smart_car_ptr> . I want to have the container take car of deleting the Cars themselves. For example calling map.erase(...) should delete the pointer as well as removing it from the map.