Bug 16412 – instance variable shadowing with inheritance
Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-08-21T20:52:02Z
Last change time
2022-11-10T13:19:56Z
Keywords
spec
Assigned to
No Owner
Creator
Lodovico Giaretta
Comments
Comment #0 by lodovico — 2016-08-21T20:52:02Z
Classes should not be allowed to shadow non-private instance variables from their base:
class Parent
{
int x = 1;
}
class Child: Parent
{
int x = 3;
}
void main()
{
Child child = new Child();
Parent parent = child;
assert(parent is child); // same object
assert(parent.x != child.x); // different value for member x
}
Comment #1 by razvan.nitu1305 — 2022-11-10T13:19:56Z
This is correct code according to the spec: https://dlang.org/spec/class.html#fields
This is not shadowing. Each class gets its own set and they don't conflict.