inheriting ctors?

Rory Mcguire rjmcguire at gm_no_ail.com
Fri Aug 6 02:43:23 PDT 2010


Sean Kelly wrote:

> Steven Schveighoffer Wrote:
> 
>> On Thu, 05 Aug 2010 13:53:20 -0400, dcoder <dcoder at devnull.com> wrote:
>> 
>> > Suppose I have a base class with many ctors().
>> >
>> > I want to inherit from the base class and make one slight alteration to
>> > it,
>> > but I don't want to write X times the following:
>> >
>> > this(args) {
>> >   super(args);
>> > }
>> >
>> > Does D have an easy way for the derived class to 'inherit' all or some
>> > of the
>> > base class ctors(), and just override/add ctors() in the derived class?
>> 
>> Sadly, no.  It's been discussed in the past.  I was surprised there were
>> no enhancement requests for it in bugzilla, care to add one?
> 
> If it helps, I wrote up something on this a few years back:
> 
> 
http://www.digitalmars.com/d/archives/digitalmars/D/Inheriting_constructors_54088.html
> 
> Might be a good starting point for an enhancement request.


I've been trying to make a template for this but it seems that dmd still 
won't allow me to get the parameters of the constructors. dmd Seems to think 
that I'm trying to use it as a property.

Using getOverloads, one should be able to generate or select any of super's 
constructors. No need for enhancement, just a bug fix. see below:

import std.traits;
import std.conv;


class A {
    int i;
    this(int x) {
        i = x;
    }
    this(float x) {
        i = cast(int)x; // ignore bad code
    }
    this(string s) {
        i = to!int(s);
    }
}
void main() {
    foreach (m; __traits(getOverloads, A, "__ctor")) {
        pragma(msg, m.stringof); // it thinks I'm calling m
    }
}

constructors.d(34): Error: constructor constructors.A.this (int x) is not 
callable using argument types ()
constructors.d(34): Error: expected 1 function arguments, not 0
this()
constructors.d(34): Error: constructor constructors.A.this (float x) is not 
callable using argument types ()
constructors.d(34): Error: expected 1 function arguments, not 0
this()
constructors.d(34): Error: constructor constructors.A.this (string s) is not 
callable using argument types ()
constructors.d(34): Error: expected 1 function arguments, not 0
this()





More information about the Digitalmars-d-learn mailing list