Suppose I have the following D code:
class Parent {
void print(int x) {
import std.stdio : writeln;
writeln(x);
}
abstract void print(string s);
}
class Child : Parent {
override void print(string s) {
import std.stdio : writeln;
writeln(s);
}
}
unittest {
Child c = new Child();
c.print(5);
}
When I compile this code, I get a compiler error stating that
function Child.print(string s) is not callable using argument types (int)
This is confusing, because I would expect that the parent's print(int x) method would be invoked when calling it on the child. I can solve this casting inline, like (cast(Parent)c).print(5), but this seems awkward.
Is this a bug in the language? Or is there a better approach to inheritance with overloaded methods?
You'll need to modify Child with an alias to Parent.print:
This is intentional. It's to prevent function hijacking. You can read about it here: https://dlang.org/articles/hijack.html
The relevant text from the section 'Derived Class Member Function Hijacking':