Is there a way to check destruction order constraints of the form "A is destructed before B" at compile time?
Asked
Active
Viewed 96 times
0
-
"(partially) Yes" for automatic objects. "No" for dynamic objects. – iammilind May 16 '12 at 09:14
3 Answers
2
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?

Yuri
- 2,008
- 17
- 36
0
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

Bo.
- 2,547
- 3
- 24
- 36
0
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.

saurabh jindal
- 121
- 4