Domanda

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

È stato utile?

Soluzione

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?

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top