On Saturday, 28 December 2019 at 20:22:51 UTC, JN wrote:
import std.stdio;
class Base
{
bool b = true;
}
class Derived : Base
{
bool b = false;
}
void main()
{
// 1
Base b = new Derived();
writeln(b.b); // true
// 2
Derived d = new Derived();
writeln(d.b); // false
}
Expected behavior or bug? 1) seems like a bug to me.
Expected. Member variables do not override base class variables.
b is declared as Base, so it knows nothing about Derived’s member
variable even though you instantiated it with an instance of
Derived. There’s no vtable for variables. If you want it to print
false, then you either have to cast b to Derived or provide a
getter function in Base that Derived can override.