alias to connect with superclass's constructor

monkyyy crazymonkyyy at gmail.com
Sat Mar 22 03:45:24 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:
>
> ```d
>     this() {
>         super():
>     }
> ```
>
> But I'm curious if there's a working example of getting this 
> effect with the compiler-recommended alias usage?  My searches 
> here and on the Duck have come up empty.
>
> ```d
> class A {
>     int a;
>
>     this() {
>         this.a = 1;
>     }
> }
> class B : A {
>     string b;
>
>     this(string s) {
>         super();
>         this.b = s;
>     }
> }
> void
> main() {
>     import std.stdio : writeln;
>
>     auto a = new A();
>     auto b1 = new B();
>     auto b2 = new B("Hi, Mom!");
>     writeln(b1.a, " - ", b1.b);
>     writeln(b2.a, " - ", b2.b);
> }
> ```

this?

```d
class A {
     int a;
     this() {
         this.a = 1;
     }
}
class B : A {
     string b;
     this(A...)(A args){
         super(args);
     }
     this(string s) {
         super();
         this.b = s;
     }
}
void
main() {
     import std.stdio : writeln;
     auto a = new A();
     auto b1 = new B();
     auto b2 = new B("Hi, Mom!");
     writeln(b1.a, " - ", b1.b);
     writeln(b2.a, " - ", b2.b);
}
```


More information about the Digitalmars-d-learn mailing list