www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - Constructor inheritance & polish function

reply Kristian <kjkilpi gmail.com> writes:
Let there be classes Base and Derived. Base is the base class of Derived=
.

There are several constructors in Base. Derived can have constructors as=
  =

follows:

1) Derived has only the default constructor which calls 'Base.this()' if=
  =

it exists.
2) Derived uses the same constructor interface as Base. The constructors=
  =

are not modified in any way (i.e. they simply call the ones in Base).
3) Derived uses the same constructor interface as Base. Some or all of t=
he  =

constructers are modified.
4) Derived uses different constructor interface as Base.

To my experience the commonness of the situations is: 2, 3, 4, and 1.


So constructors should be inherited from the base class. For example:

class Base {
     this() {...}
     this(int val) {...}

     void f() {...}
}

class Derived : Base {
     void f() {...}  //overrule a member function
}

void func() {
     Derived obj =3D new Derived(1);  //calls 'Base.this(int)' (now a co=
mpile  =

time error)
}

It would make the case 2 breeze to implement!

If you define constructors for Derived, then the ones defined in Base  =

should be hidden. The case 1 could be achieved by defining one  =

constructor: 'this() {}'.


If there was a polish function (just like one in Qt) which is called aft=
er  =

a constructor is finished, it would save unnecessary constructor writing=
  =

in some cases. It would be especially useful when Derived is using the  =

same constructor interface as Base (case 2) except the constructors shou=
ld  =

do something additional also (subtype of case 3). For example:

class Base {
     this() {
         m_val =3D -1;
         }
     this(int val) {
         m_val =3D val;
         }

     void f() {
         m_val =3D 100;
         }

     int m_val;
}

class Derived : Base {
     this_polish() {  //is called after a constructor
         f();
         }
}

void func() {
     Derived obj =3D new Derived(1);  //'obj.m_val' =3D=3D 100 (first 1,=
 then 100)
}


This would reduce redundant code, which is a possible source for typos a=
nd  =

errors. It would also be easier to maintain: if parameter lists of Base'=
s  =

constructors are modified, no changes will be required for Derived.
Aug 21 2006
parent Kristian <kjkilpi gmail.com> writes:
Sorry, wrong news group!
Aug 21 2006