private method in interface

Michael Shulman viritrilbia at gmail.com
Fri Jun 3 19:44:11 PDT 2011


On Fri, Jun 3, 2011 at 3:07 PM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> I'd suggest opening a bug report.

Okay, will do.

> Regardless, I would have hoped that you'd get an error at compile
> time about overriding a non-virtual function if package is
> non-virtual

It appears that overriding a non-virtual, non-abstract method (private
or package) just hides the base-class version when the reference is of
derived type, exactly as happens in C++ for non-virtual methods.  E.g.

class A {
  private void dostuff() { writeln ("In A.dostuff"); }
}

class B : A {
  private void dostuff() { writeln ("In B.dostuff"); }
}

void main () {
  B y = new B();
  A x = y;
  y.dostuff();
  x.dostuff();
}

prints

In B.dostuff
In A.dostuff

I would have kind of hoped for a compiler error too, given that C++
programmers generally seem to feel that overriding non-virtual methods
in this way is a bad idea.

Mike


More information about the Digitalmars-d-learn mailing list