What is going on here?

Shachar Shemesh via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 4 21:54:14 PST 2015

On 05/03/15 04:09, deadalnix wrote:
> On Wednesday, 4 March 2015 at 19:36:20 UTC, Ali Çehreli wrote:
>> Even C++ gets this right.
>> Ali
> Nop, I do think C++ destroy uninitialized fields.

I'm sorry, but you are wrong.

In C++, a destructor is run for "fully constructed" objects. Fully 
constructed is defined to be an object whose constructor has finished 
successfully (i.e. - without throwing).

Construction order of an object is:
1. Parent(s), if any, in no particular order (unless employing virtual 
inheritance, in which case a virtual grandparent is constructed before 
its descendants)
2. Members, in the order in which they were defined in the class (not 
the order in which they are listed in the constructor. gcc gives a 
warning if the two are not the same)
3. The actual object

This means that all members are parents are fully constructed by the 
time the constructor body starts to run, which means that if it throws, 
the class itself is the only one for which a destructor isn't going to 
get called.

This also has the less desirable effect that if you want to call a 
member's non-default constructor, you need to have all arguments ready 
before the class' constructor starts to run.

Personally, I think the first is well worth the occasional case where 
the second becomes a liability.


More information about the Digitalmars-d mailing list