Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 21, 2016 Constructor Inheritance | ||||
---|---|---|---|---|
| ||||
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`? |
June 21, 2016 Re: Constructor Inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | no and no. howewer, creating template mixin with default ctors may spare you of some typing. |
June 20, 2016 Re: Constructor Inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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." |
June 21, 2016 Re: Constructor Inheritance | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | 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).
|
Copyright © 1999-2021 by the D Language Foundation