Class children pointers into a vector of the parent class

Hi, I have been searching the forum but found no solution to my problem.

I have two vectors(one in the garage class and on in the car registry(transportstyrelsen) class) which stores pointer of a Parent class.
I only use them the store the different children that derived from the parent class and I move them between this vectors(it's a garage).

So I wanna clear the memory of the objects that been created on the heap.

When I clear/ delete the children objects in the garage class everything is fine but not when I try to clear the car registry

I think my problem is that when I add objects in the TransportStyrelens Vector I do it like this: TransportStyrelsenDatabaseInfo->push_back(new Car("ABC123", "Red", 4, CAR, false, true));

How Can I make sure it is a Vehicle pointer of the child class for example car that is being created on the heap. For all, I know this might be a Car pointer to a new Car.
What syntax do you use to make a new Car of the pointer Type Vehicle in a push_back. ?


This is the destructor for the garage:
class Garage
{
public:
Garage(); // The constructor that take how many parking spots you garage will have.
~Garage();
bool addVehicle(Vehicle*); // A method thats try to park a 'Vehicle' and return 'true' if successful.
void listSpots(); // List whats on every parking spot
void typeOfVehicles(); // List the sum of the different types
bool retractVehicle(int); // Retracts vehicle from parking spot
string getGarageName();
//General Vehicles search
void searchRegisterNumber();//Search for a vehicle with a certain regnumber
void searchWheelsMoreThenTwo();// Search for vehicles with more then two wheels
//Car specific searches
void searchBattery();// Search the Vector and checks for cars with batteries
void searchConvertable(); // Search the vector for cars with convertibles
//Lorry Info
void searchLorry(); // Gives info about parked lorries

protected:

private:
string GarageName;
int maxGarageSize{}; // The max size/spots of the garage
vector<Vehicle *> * parkingSpots = new vector<Vehicle *>{}; // A pointer to a vector that contains pointers of 'Vehicle's

};
Garage::~Garage()
{
cout << "Unloading garage from memory: " << endl;
for (vector<Vehicle*>::iterator it = parkingSpots->begin(); it != parkingSpots->end(); it++)
{
if (*it != NULL)
{
delete (*it);
}
}
//parkingSpots->clear;
cout << "Done!" << endl;
}

This is the destructor for the car registry:
class Transportstyrelsen
{
public:
vector<Vehicle *> * TransportStyrelsenDatabaseInfo = new vector<Vehicle *>{};
Transportstyrelsen();
~Transportstyrelsen();
void listTransportStyrelsenDatabase();
void addToTransportStyrelsensDatabase();


private:

};

Transportstyrelsen::~Transportstyrelsen()
{
cout << "Unloading transportstyrelsen database: " << endl;
for (vector<Vehicle*>::iterator it = TransportStyrelsenDatabaseInfo->begin(); it != TransportStyrelsenDatabaseInfo->end(); it++)
{
if (*it != NULL)
{
delete (*it);
}
}
cout << "Done." << endl;
}


Do the garage and car registry point to the same cars? If so, you are trying to delete the same memory multiple times.

Make sure you know who is responsible for deleting the cars when they are done. Don't have both sides do it.

You should consider using unique_ptr or shared_ptr instead of raw pointers in your code. Those classes handle a lot of this situation for you.
WOW ofc thats the thing... ofc its gets errors when they delete the cars in garage first... Maybe its best they dont ever delete the garage since they all are in the car registry...
Thanks man Ill try it out.

Yes I will move on to shared and uniq pointers later im in the learning progress :)
You were right thanks you for the solution!
Topic archived. No new replies allowed.