mercredi 23 mai 2018

How to execute some code in destructor *after* member are destroyed

I have a "manager" class that has to do certain clean-up after the members are destroyed:

class Manager
{
    Member a, b;

    ~Manager()
    {
        // this code will be executed BEFORE the a and b destructors
        important_cleanup();
    }
};

The problem here is that important_cleanup() invalidates my variables a and b, so their destructors fail.

Is there a nice way around the problem?

PS I know I can define some kind of clenup member, and make it be the first created member of Manager, but this looks quite ugly and makes things order-dependent...

Aucun commentaire:

Enregistrer un commentaire