I have a program that contains a doctor class, and each Doctor object has a linked list of “Patients”. These patients are created using the following line of code
Patient * patient = new Patient(string firstname, string ailment);
And this patient is then added to a Doctor’s linked list.
There is a pointer in the Doctor class that is used to point to and iterate through the list and get each patient. When I want to delete these patients, I have to start at the beginning of my list and iterate through each one. My question is, can I delete eacg patient by simply calling their destructors as I iterate through them?
Patient::~Patient(){
}
or do I need to actually call delete on the pointer as it points each employee?
You call
deleteto delete things.deletewill, in turn, result in the object’s destructor being called.Calling the destructor of a class directly is supported by C++, and is in fact useful in some rare scenarios, but it does not actually deallocate the memory. This is not what you want to do here.