constructor inheritance

Robert Fraser fraserofthenight at gmail.com
Tue Mar 4 13:25:15 PST 2008


Simen Kjaeraas wrote:
> On Tue, 04 Mar 2008 21:59:35 +0100, Elwis <elwispl at gmail.com> wrote:
> 
>> Ary Borenszweig Wrote:
>>
>>> Elwis wrote:
>>> > I might have described my problem unclearly.
>>> >
>>> > I has root class and it has some constructor. One of those just 
>>> calls one of its methods. I'd like not to copy declaration of this 
>>> constructor in all of its children.
>>>
>>> Do you mean you don't want to have to do this?
>>>
>>> class Parent {
>>>
>>>    this(int x, int y) {
>>>      // some code
>>>    }
>>>
>>> }
>>>
>>> class Child : Parent {
>>>
>>>    // I wish the compiler would add the this(int x, int y) constructor
>>>    // automatically for me here
>>>
>>> }
>>
>> It doesn't work. Maybe it isn't supported by GDC?
> 
> 
> It is not supported by D at all. At least not at the moment. Like I 
> said, your best bet is templates.
> Something like
> 
> template constructors()
> {
>     this()
>     {
>         // do stuff here
>     }
>     
>     this(int x, int y)
>     {
>         // do stuff here
>     }
> }
> 
> class Parent
> {
>     mixin constructors;
> }
> 
> class Child
> {
>     mixin constructors;
> }

How about just:

class Child : Parent
{
     this(int x, int y)
     {
         super(x, y);
         // Do anything else here if you want
     }
}



More information about the Digitalmars-d mailing list