if the constructor throws an exception, which destructors are triggered?
Destructors of all objects completely created in this area.
Does it matter if the exception is during the initialization list or body?
All completed objects will be destroyed.
If the constructor was never fully invoked, the object was never built and therefore could not be destroyed.
what about inheritance and members? Presumably, all completed structures are destroyed. If only some members are created, delete only those who destroy them? If multiple inheritance exists, do all the destroyed constructors end? Does virtual inheritance change anything?
All completed structures are destroyed. Yes, only completely created objects are destroyed.
Good reading:
Constructor Failure from Herb Sutter
Especially, love the part where he explains:
In biological terms, a concept arose - the designer began - but, despite all efforts, he was followed by a miscarriage - the designer never approached the terms (ination).
By the way, this is why the destructor will never be called if the constructor is unsuccessful - there is nothing to destroy. "He cannot die because he never lived." Note that this makes the phrase "an object whose constructor throws an exception" really an oxymoron. Such a thing is even smaller than the ex-object ... he never lived, never was, never breathed first.
Alok Save Apr 18 '12 at 15:35 2012-04-18 15:35
source share