Post-ctor ctor

bearophile bearophileHUGS at lycos.com
Sun Aug 7 12:16:08 PDT 2011


Trass3r:

> >     this(int this.a, int this.b, int this.c, int this.d) {
> >         sum = a + b + c + d;
> >         average = (a + b + c + d) / 4;
> >     }
> 
> Can't express how awkward this is.

Why don't you try to express how awkward it is?

Well, code like this is shorter than what the OP has suggested, it's DRY, it's probably easy to understand (I think you don't need to go look into D docs every time you see something like this), it's easy to type, it avoids a common bug in my code (caused by mixing fields and arguments in the constructor):

this(this.a, this.b, this.c, this.d) {
    sum = a + b + c + d;
    average = (a + b + c + d) / 4;
}

Bye,
bearophile


More information about the Digitalmars-d mailing list