hiding a class property behind a method
Maxim Fomin
maxim at maxim-fomin.ru
Sat Feb 22 10:00:44 PST 2014
On Saturday, 22 February 2014 at 17:41:58 UTC, Ali Çehreli wrote:
>
> The code uses the two objects through the A interface and x()
> is a virtual function on that interface.
>
> When the C interface is used then we get C.x, which happens to
> be hiding the x() function of the base class.
>
> It looks normal to me.
>
> Ali
Spec is silent on this, so this is indeed a question.
Actually A is not interface, so I don't understand why you
mention it. And there is neither 'taking C interface' because
static type is A, so A function is called, neither it hides
function of the base class because it is base class function
which is called. I don't understand you completely.
AFAIK this feature exists for many years, at least 3, possibly
roots to D1. What happens is follows: since there is no function,
base class virtual is not replaced, so virtual table of C looks
like A, so A member function is called.
If example is modified, then
import std.stdio;
class A {
//string x () { return "A"; };
string x = "A";
}
class B : A {
//override string x () { return "B"; };
string x = "B";
}
class C : A {
//string x = "C"; // should this be illegal?
string x () { return "C"; }
}
void main () {
A o1 = new B();
A o2 = new C();
writeln(o1.x); // A
writeln(o2.x); //A
}
so it appears that data member have priority over function.
Probably this should be filed as a spec or compiler bug.
More information about the Digitalmars-d-learn
mailing list