Frage

Is there a way to check destruction order constraints of the form "A is destructed before B" at compile time?

War es hilfreich?

Lösung

I don't think that's possible. Take for instance the following code:

int main(int argc, char **argv){
  Object A = new Object(), B = new Object();
  if(argc == 1){
    delete A;
    delete B;
  }else
  {
    delete B;
    delete A;
  }
}

How would you know at compile time which destructor is called first?

Andere Tipps

You can check that easily by adding commands to the destuctor. See(for example here for more detailed description): http://msdn.microsoft.com/en-us/library/8183zf3x(v=vs.80).aspx

Kind regards, Bo

It is fixed for auto variables and can not be fixed for dynamic allocated objects. Also, if your question is for member objects of the class, then yes it is fixed. The members which are listed first in the class declaration, they are destroyed first.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top