As a human, i'm lazy, so if i need to
write a class with three constructors,
i'd like to minimize the work. I'm used
to two different approaches and i'd
like to get an opinion on which is to
be most preferred.

// 1. Constructors calling each other.
class C {
C () {this (5);}
C (int i) {this (5, 3);}
C (int i, int j) {} }

// 2. Constructors calling a common method.
class C {
C () {common (5, 3);}
C (int i) {common (i, 3);}
C (int i, int j) {common (i, j);}
void common (int i, int j) {} }

My applogies for the ugly code. I tried to
keep it short.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Re: Cascading through the constructors by Jon

Jon
Sat Mar 15 15:43:44 CDT 2008

K Viltersten <tmp1@viltersten.com> wrote:
> As a human, i'm lazy, so if i need to
> write a class with three constructors,
> i'd like to minimize the work. I'm used
> to two different approaches and i'd
> like to get an opinion on which is to
> be most preferred.
>
> // 1. Constructors calling each other.
> class C {
> C () {this (5);}
> C (int i) {this (5, 3);}
> C (int i, int j) {} }
>
> // 2. Constructors calling a common method.
> class C {
> C () {common (5, 3);}
> C (int i) {common (i, 3);}
> C (int i, int j) {common (i, j);}
> void common (int i, int j) {} }
>
> My applogies for the ugly code. I tried to
> keep it short.

I personally usually use option 1 - although I vary between a "long"
constructor chain (where the parameterless calls the single parameter,
which calls the double parameter) and "short" constructor chains (where
every constructor calls the most complicated one).

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

SV: Cascading through the constructors by K

K
Sat Mar 15 16:50:38 CDT 2008

>> Which is to be most preferred?
>>
>> // 1. Constructors calling each other.
>> class C {
>> C () {this (5);}
>> C (int i) {this (5, 3);}
>> C (int i, int j) {} }
>>
>> // 2. Constructors calling a common method.
>> class C {
>> C () {common (5, 3);}
>> C (int i) {common (i, 3);}
>> C (int i, int j) {common (i, j);}
>> void common (int i, int j) {} }
>>
>> My applogies for the ugly code. I tried to
>> keep it short.
>
> I personally usually use option 1 - although
> I vary between a "long" constructor chain
> (where the parameterless calls the single
> parameter, which calls the double parameter)
> and "short" constructor chains (where every
> constructor calls the most complicated one).

Thanks!

Just one more thing. I don't seem to be able
to compile

C () {this (5, 3);}

as the compiler nags about "method name
expected". I expected the same syntax as in
Java, perhaps foolishly... Help?

I suspect i could use the syntax:

C () : this (5, 3) {}

but i don't want to risk it. I want to make
sure that the LAST thing done in the simple
constructor is calling the complicated one,
not the first thing.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy


Re: SV: Cascading through the constructors by Jon

Jon
Sat Mar 15 16:52:05 CDT 2008

K Viltersten <tmp1@viltersten.com> wrote:
> Thanks!
>
> Just one more thing. I don't seem to be able
> to compile
>
> C () {this (5, 3);}
>
> as the compiler nags about "method name
> expected". I expected the same syntax as in
> Java, perhaps foolishly... Help?

You've got it here:

> I suspect i could use the syntax:
>
> C () : this (5, 3) {}
>
> but i don't want to risk it. I want to make
> sure that the LAST thing done in the simple
> constructor is calling the complicated one,
> not the first thing.

No, it's just like Java in that respect (though not in the syntax) -
the chained constructor call is always the first thing to be executed.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

SV: SV: Cascading through the constructors by K

K
Sat Mar 15 17:16:43 CDT 2008

>> Just one more thing. I don't seem to be able
>> to compile
>>
>> C () {this (5, 3);}
>>
>> as the compiler nags about "method name
>> expected". I expected the same syntax as in
>> Java, perhaps foolishly... Help?
>
> You've got it here:
>
>> C () : this (5, 3) {}


Are you saying that i can't call a constructor
in a class C from INSIDE an other constructor
in the same class C?! That's surprising...

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy


Re: SV: SV: Cascading through the constructors by Jon

Jon
Sat Mar 15 17:36:34 CDT 2008

K Viltersten <tmp1@viltersten.com> wrote:
> > You've got it here:
> >
> >> C () : this (5, 3) {}
>
>
> Are you saying that i can't call a constructor
> in a class C from INSIDE an other constructor
> in the same class C?! That's surprising...

Well, bear in mind that every constructor is meant to *eventually* call
a base class constructor (potentially though chaining) - and that is
designed to happen before any other code in your class is called (aside
from variable initializers).

As I say, the same restriction is in Java. I don't think the CLR
requires it, admittedly.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

SV: SV: SV: Cascading through the constructors by K

K
Sat Mar 15 19:07:24 CDT 2008

>> Are you saying that i can't call a constructor
>> in a class C from INSIDE an other constructor
>> in the same class C?! That's surprising...
>
> Well, bear in mind that every constructor is meant
> to *eventually* call a base class constructor
> (potentially though chaining) - and that is
> designed to happen before any other code in your
> class is called (aside from variable initializers).
>
> As I say, the same restriction is in Java. I don't
> think the CLR requires it, admittedly.

It's been a while i've been working with Java but i
remember that the construction was as follows.

class J {
public J () {this(5);}
public J (int i) {} }

I.e., the call to a fellow constructor has been
placed INSIDE the body and one could, as far my
memory serves me, execute statements BEFORE the
call to the constructor.

The call to the base, however, wasn't so freely
placeable. It had to come first in the constructor.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy


Re: SV: SV: SV: Cascading through the constructors by arne

arne
Sat Mar 15 19:32:55 CDT 2008

K Viltersten wrote:
> It's been a while i've been working with Java but i
> remember that the construction was as follows.
>
> class J {
> public J () {this(5);}
> public J (int i) {} }
>
> I.e., the call to a fellow constructor has been
> placed INSIDE the body and one could, as far my
> memory serves me, execute statements BEFORE the
> call to the constructor.

The first is correct. You call this inside {}.

The second is not correct. You can not call
other statements before this.

Arne

Re: SV: SV: SV: Cascading through the constructors by Jon

Jon
Sun Mar 16 02:37:02 CDT 2008

K Viltersten <tmp1@viltersten.com> wrote:
> It's been a while i've been working with Java but i
> remember that the construction was as follows.
>=20
> class J {
> public J () {this(5);}
> public J (int i) {} }
>=20
> I.e., the call to a fellow constructor has been
> placed INSIDE the body=20

Correct.

> and one could, as far my
> memory serves me, execute statements BEFORE the
> call to the constructor.

Incorrect. From the spec=20
(http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html)

<quote>
The first statement of a constructor body may be an explicit invocation=20
of another constructor of the same class or of the direct superclass (=A7
8.8.7.1).

ConstructorBody:
=09{ ExplicitConstructorInvocationopt BlockStatementsopt }

</quote>

--=20
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

SV: SV: SV: SV: Cascading through the constructors by K

K
Sun Mar 16 03:28:21 CDT 2008

>> It's been a while i've been working with Java but i
>> remember that the construction was as follows.
>>
>> class J {
>> public J () {this(5);}
>> public J (int i) {} }
>>
>> I.e., the call to a fellow constructor has been
>> placed INSIDE the body and one could, as far my
>> memory serves me, execute statements BEFORE the
>> call to the constructor.
>
> The first is correct. You call this inside {}.
>
> The second is not correct. You can not call
> other statements before this.


Then i guess i was mistaken. It's been a while
since i've touched anything bean-related, so i'll
stand corrected.

In that case, thank you both for the correction.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy