alias to connect with superclass's constructor

Salih Dincer salihdb at hotmail.com
Mon Mar 24 03:46:25 UTC 2025


On Saturday, 22 March 2025 at 03:35:35 UTC, Andy Valencia wrote:
> Consider the following, totally contrived, code.  The compiler 
> tells me:
>
> tst39.d(21): Error: constructor `tst39.B.this(string s)` is not 
> callable using argument types `()`
> tst39.d(21):        constructor `tst39.B.this` hides base class 
> function `tst39.A.this`
> tst39.d(21):        add `alias this = tst39.A.this` to 
> `tst39.B`'s body to merge the overload sets
>
> But I haven't yet found a way to use this guidance to resolve 
> the error.  I _can_ add this to B:
>

When you add an empty constructor, the code still runs without an 
error

```d
class Counter : StaticCounter {
     string msg;

     this(string str) { msg = str; }
     this() {} // added Salih
}

class StaticCounter {
     static int id;
     this() { ++id; }
}

import std.stdio : writeln;
void main() {
     with ( new Counter() )
         id.writeln(": ", msg);

     with ( new Counter("Hi, Mom!") )
         id.writeln(": ", msg);
}
```

SDB at 79


More information about the Digitalmars-d-learn mailing list