   Exception clarification (follow-up question in second reply)
  Can I get feedback on whether my understanding is correct or not, please?I have some class A, and some class B. Class B is composed of a further class C.I create an instance of A in some scope S1, and then an instance of class B.In the constructor for B, the class C it is composed of is constructed. An exception is then thrown. The destructor of the member C is then called, the destructor of the B instance is not called.The exception is not handled in S1, so the A instance has the destructor called, and then we continue up scopes until somebody (hopefully) handles it.In short: all classes that successfully constructed are successfully deconstructed. The B did not successfully construct, and so is not deconstructed (and so if it contained a pointer to an object that was newed before the exception, but inside the B constructor, this object would not be deconstructed and the pointer lost, and so we would leak memory).Right?[Edited by - TheUnbeliever on August 30, 2007 4:50:08 PM]
  That's correct, but just to be clear: even though B's destructor won't get called in this situation, any members of B that were constructed before C threw the exception will be destructed in reverse order of construction.Edd   ;Quote:Original post by the_eddThat's correct, but just to be clear: even though B's destructor won't get called in this situation, any members of B that were constructed before C threw the exception will be destructed in reverse order of construction.I meant that B's constructor threw the exception, but after C was constructed, so that was what I thought. Thanks for the clarification — I just wanted to make sure I fully understood the circumstances under which RAII functions (looking at getting my code to a point whereby I'm designing away from potential problems).If they are destructed in reverse order of construction, is there a set order of construction (if so, presumably the order in which they are declared)?   ;  Yeah the order of construction is specified and is the order of declaration.  In particular it is *not* the order of initialization if you use the initialization syntax in the constructor.  This causes problems occasionally and last I checked the compiler didn't complain if you initialized stuff in a different order than it was declared.For multiple inheritance I think the order is simply that it intializes subclasses from left to right based on the declaration.  I avoid multiple inheritance most of the time though so I could be wrong.   ;  Thank you. I also tend to avoid multiple inheritance, but I'll check up on what you said.   ;Quote:Original post by Anon MikeYeah the order of construction is specified and is the order of declaration.  In particular it is *not* the order of initialization if you use the initialization syntax in the constructor.  This causes problems occasionally and last I checked the compiler didn't complain if you initialized stuff in a different order than it was declared.I'm pretty sure all popular compilers can at least be asked to give a warning for this.   ;  Every version of MSVC I have installed on my computer right now lets this go undiagnosed at /W4:struct C {  int a;  int b;  C(int x) : b(x), a(b) {}};