mercredi 27 juillet 2016

How do I deallocate all derived dynamic memory of a member?

Say I have a class:

struct foo{
    foo* bar=nullptr;
};

Now if I use new to allocate memory like a chain:

foo instance;
instance.bar= new foo;
instance.bar->bar=new foo;
...

Then how can I delete all those children of instance in a single call to delete the top level instance, i.e. when I call destroy(instance); //dummy name then all those dynamically allocated memory are all freed?

Aucun commentaire:

Enregistrer un commentaire