Is this a DMD bug, or correct behavior?
Adam D. Ruppe
destructionator at gmail.com
Wed Mar 13 00:50:35 UTC 2019
On Wednesday, 13 March 2019 at 00:27:02 UTC, Foo Bar wrote:
> I came across this strange behavior, however I am unsure as to
> whether this is correct behavior, or just an expected behavior
> of shadowing.
Yes, it is correct behavior, though it does trip a lot of people
up.
Each variable set in a class declaration is independent. Lookups
go for the first one they see up a chain from the static type.
Inside the override foo, it looks up and sees B.boolean. But
outside, you cased to A, so it can only see A.boolean.
Object-oriented pattern here is typically to keep your member
vars private and read/write through accessor methods instead,
which can be virtual and thus be overridden in child classes as
needed.
More information about the Digitalmars-d
mailing list