Struct inheritance

Derek Fawcus dfawcus+dlang at employees.org
Wed Dec 4 11:38:07 UTC 2024


(That was a bit iffy, try this one instead)

On Wednesday, 4 December 2024 at 11:10:59 UTC, Derek Fawcus wrote:
> So what would your scheme do for the struct equivalent of this, 
> assuming it is even compiles:


```D
import std.stdio;

void main() {
     Parent parent = new Parent;
     parent.methB("Direct");

     writeln();
     Child child = new Child;
     child.methB("Inherit");

     writeln();
     parent = child;
     parent.methB("Assign");
}

class Parent {
     void methB(string s) {
      	writeln("MethB(P) ", s, " ", typeof(this).stringof);
	methC(s);
     }
     void methC(string s) {
      	writeln("MethC(P) ", s, " ", typeof(this).stringof);
     }
}

class Child : Parent {
     override void methC(string s) {
      	writeln("MethC(C) ", s, " ", typeof(this).stringof);
     }
}
```

For which I get:

```
MethB(P) Direct Parent
MethC(P) Direct Parent

MethB(P) Inherit Parent
MethC(C) Inherit Child

MethB(P) Assign Parent
MethC(C) Assign Child
```


More information about the dip.ideas mailing list