Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ deleting an object

Does the delete() operator destroy all the sub-objects of an object too? Or do I need to call the delete of sub-object before deleting the parent object?

class equipment
{
   public:
     int model_id;
     ...
}

class player
{
   public:
     int x, y;
     equipment * Equipment; 
     player(void) { Equipment = new equipment[2];};
     ~player(void) { delete [] Equipment; }
};

int main (....)
{
  player = new Player;
  ...
  ...
  delete Player;
}
like image 983
Wracker Avatar asked May 03 '26 14:05

Wracker


1 Answers

you need to delete the dynamically allocated subobjects in the destructor of the main object and you have properly done this by deleting array Equipment

like image 126
fatihk Avatar answered May 06 '26 03:05

fatihk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!