Search code examples
c++delete-operator

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;
}

Solution

  • 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