import std.stdio;
class Base
{
string f()
{
return "Base.f()";
}
}
class Derived : Base
{
string f()
{
return "Derived.f()";
}
string f() const
{
return "Derived.f() const";
}
}
void main()
{
auto x = new Base;
writeln(x.f());
auto y = new Derived;
writeln(y.f());// calls "Derived.f() const", but it is expected that be called non-const.
auto z = new const(Derived);
writeln(z.f());
}
Comment #1 by schveiguy — 2011-10-31T08:02:36Z
This is neither invalid nor fixed. Tested in Linux 32-bit.
Tested with 2.055:
Base.f()
Derived.f() const
Derived.f() const
Tested with 2.056:
Base.f()
Derived.f()
Segmentation fault
Clearly there is some invalid vtable stuff going on here. Probably was going on before, but manifested in a different way. If I remove the base class from the picture, it correctly prints out:
Derived.f()
Derifed.f() const
Expected printout for original code should be:
Base.f()
Derived.f()
Derived.f() const
Comment #2 by bugzilla — 2012-01-28T00:17:42Z
This is an undiagnosed error. Both Derived.f functions override Base.f.
Comment #3 by github-bugzilla — 2012-01-28T00:23:19Z
(In reply to comment #2)
> This is an undiagnosed error. Both Derived.f functions override Base.f.
The first Derived.f overrides Base.f, the second is an additionally introduced overload. According to http://www.d-programming-language.org/function , this should not be a compile error but behave as stated in the bug report.
Comment #7 by timon.gehr — 2012-01-28T14:05:08Z
On second thought, I might be wrong. The spec states:
- "Virtual functions all have a hidden parameter called the this reference, which refers to the class object for which the function is called."
- "A functions in a derived class with the same name and parameter types as a function in a base class overrides that function:"
If the type of the hidden parameter was included in the "parameter types" part of the second statement, no function would override another, therefore it apparently is not. Therefore the commit indeed fixed the discrepancy with the spec. I think the design should be revisited at some point to make overriding more intuitive and powerful.