www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19943] New: Field base access works for virtual methods

https://issues.dlang.org/show_bug.cgi?id=19943

          Issue ID: 19943
           Summary: Field base access works for virtual methods
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: minor
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: default_357-line yahoo.de

The following code outputs "A":

import std.stdio;

class A {
    int a;
    void foo() { writefln("A"); }
}
class B : A { 
    int a;
    override void foo() { writefln("B"); }
}

void main() { (new B).A.foo(); }

This defeats the entire point of classes. This is a side effect of
https://dlang.org/spec/class.html#fields , which specifies that (new B).A.a
accesses the "int a" of the base class. That's fine, but this feature should
not work for virtual methods, which the subclass explicitly overrides. Only the
subclass should be allowed to access super methods.

--
Jun 05 2019