Check destruction order constraints at compile time

112 views Asked by At

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

3

There are 3 answers

1
Yuri On BEST ANSWER

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?

1
Bo. On

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

0
saurabh jindal On

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.