www.digitalmars.com         C & C++   DMDScript  

D - D can have one constructor call another one

reply "Klaus Eichner" <klaus_gb yahoo.com> writes:
I have played around with C++ in the past years, when I suddenly came across
the D-compiler. I had a look at the web-page
http://www.digitalmars.com/d/index.html, and I must say that many of the
features look very promising to me (and, in my view, would also be
beneficial to C++).

To name but a few:
- modules,
- properties,
- contracts,
...

However, there is one aspect in D which I find confusing, and this is:

D can have one constructor call another one:
----------------------------
class A
{ int a = 7;
   int b;
   this() { b = foo(); }
   this(int x)
  {
      this();
      a = x;
  }
}
----------------------------

I must say that this examples take me back to my early experiences of
programming (that was COBOL-74 in the 1980's) where I spent 80% of my time
tracing back someone else's 10,000 line COBOL listing to identify which
initialisation section was initialising which field. Later, when I stumbled
over C++, I learned that C++ does not allow one constructor to call another
one. At first view, this looked restricting when writing new code, but as I
soon found out, this rule saved me a lot more of development time when it
came to maintenance and error tracing.

I am a little bit surprised to find that D allows such things.

OK, I admit it, we are only talking about constructors and not about a fully
fledged 1980's 10,000-line COBOL-74 program, and the
"one-constructor-calls-another-one" principle adds a certain flexibility,
but I find that the benefits (of not allowing one constructor to call
another one) by far outweigh the costs.

Any thoughts on this ?

Regards
Klaus
Aug 12 2003
parent "Sean L. Palmer" <palmer.sean verizon.net> writes:
If you don't have constructors able to call other constructors, you end up
being forced into having private init() methods that both ctors call.

I'd just as soon have the complex ctor be able to call the default ctor if
it wants, before doing the complex thing.  Reduces code redundancy.

Sean

"Klaus Eichner" <klaus_gb yahoo.com> wrote in message
news:bhbbkc$1h5j$1 digitaldaemon.com...
 I have played around with C++ in the past years, when I suddenly came

 the D-compiler. I had a look at the web-page
 http://www.digitalmars.com/d/index.html, and I must say that many of the
 features look very promising to me (and, in my view, would also be
 beneficial to C++).

 To name but a few:
 - modules,
 - properties,
 - contracts,
 ...

 However, there is one aspect in D which I find confusing, and this is:

 D can have one constructor call another one:
 ----------------------------
 class A
 { int a = 7;
    int b;
    this() { b = foo(); }
    this(int x)
   {
       this();
       a = x;
   }
 }
 ----------------------------

 I must say that this examples take me back to my early experiences of
 programming (that was COBOL-74 in the 1980's) where I spent 80% of my time
 tracing back someone else's 10,000 line COBOL listing to identify which
 initialisation section was initialising which field. Later, when I

 over C++, I learned that C++ does not allow one constructor to call

 one. At first view, this looked restricting when writing new code, but as

 soon found out, this rule saved me a lot more of development time when it
 came to maintenance and error tracing.

 I am a little bit surprised to find that D allows such things.

 OK, I admit it, we are only talking about constructors and not about a

 fledged 1980's 10,000-line COBOL-74 program, and the
 "one-constructor-calls-another-one" principle adds a certain flexibility,
 but I find that the benefits (of not allowing one constructor to call
 another one) by far outweigh the costs.

 Any thoughts on this ?

 Regards
 Klaus

Aug 13 2003