[Issue 19943] New: Field base access works for virtual methods

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jun 5 21:29:02 UTC 2019


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 at puremagic.com
          Reporter: default_357-line at 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.

--


More information about the Digitalmars-d-bugs mailing list