I don’t know if I should create a destructor to delete the members of Controller that are set to the parameters passed by main which are created dynamically, because the constructor never used the new keyword to set them.
int main()
{
int numCars = 3;
int numPlanes = 3;
Machine *car= new Car[numCars];
Machine *plane = new Plane[numPlanes];
Controller *control = new Controller(car, plane);
delete control;
return 0;
}
class Controller
{
public:
Controller(Machine *car, Machine *plane);
//Would I need to make a destructor
//~Controller();
private:
Machine *car;
Machine *plane;
Controller :: Controller(Machine *car, Machine *plane)
{
this->car = car;
this->plane = plane;
changeCarandPlane();
}
//destructor
// Controller :: ~Controller()
// {
// delete[] car;
// delete[] plane;
// }
};
No you shouldn’t delete in the destructor.
mainseems to own the allocated arrays, so delete frommain.If you were to delete in the
Controllerdestructor, you would have to ensure that no two instances ofControllerpoint to the same dynamically allocatedMachinearrays.You should consider using
std::vector<Machine>instead of dynamically allocated arrays, or at least some scope guards to guarantee deletion.