D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 16412 - instance variable shadowing with inheritance
Summary: instance variable shadowing with inheritance
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 enhancement
Assignee: No Owner
URL:
Keywords: spec
Depends on:
Blocks:
 
Reported: 2016-08-21 20:52 UTC by Lodovico Giaretta
Modified: 2022-11-10 13:19 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Lodovico Giaretta 2016-08-21 20:52:02 UTC
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 RazvanN 2022-11-10 13:19:56 UTC
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.