www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Constructor Inheritance

reply Meta <jared771 gmail.com> writes:
It's been so long since I've had to use OOP in D that I'm 
starting to forget things like this. If I have the parent class A 
which defines a constructor:

class A
{
     string val;

     this(string val) { this.val = val; }
}

And a child class B which inherits from A:

class B: A
{
}

I get the the following error:

Error: class B cannot implicitly generate a default ctor when 
base class A is missing a default ctor

Is there a simpler way to do this without having to define a 
constructor in B that just forwards to A's constructor, like so?

class B: A
{
     this(string val) { super(val); }
}

Is it allowed to do something like `alias __ctor = super.__ctor`?
Jun 20 2016
parent reply ketmar <ketmar ketmar.no-ip.org> writes:
no and no. howewer, creating template mixin with default ctors 
may spare you of some typing.
Jun 20 2016
parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Tue, Jun 21, 2016 at 02:48:39AM +0000, ketmar via Digitalmars-d-learn wrote:
 no and no. howewer, creating template mixin with default ctors may
 spare you of some typing.
I think Phobos has an AutoImplement template that might do this for you. Maybe take a look in std.typecons or std.meta (or wherever they shove those things these days)? T -- "Hi." "'Lo."
Jun 20 2016
parent Meta <jared771 gmail.com> writes:
On Tuesday, 21 June 2016 at 03:06:22 UTC, H. S. Teoh wrote:
 On Tue, Jun 21, 2016 at 02:48:39AM +0000, ketmar via 
 Digitalmars-d-learn wrote:
 no and no. howewer, creating template mixin with default ctors 
 may spare you of some typing.
I think Phobos has an AutoImplement template that might do this for you. Maybe take a look in std.typecons or std.meta (or wherever they shove those things these days)? T
I was going to say that this is painful compared to Java or C++, but it looks like it's been too long since I've used those languages as well; neither of them allow constructor inheritance (but C++ has a syntax for doing it explicitly).
Jun 20 2016