Override with function overloads

nkm1 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 11 10:59:25 PDT 2017


On Monday, 11 September 2017 at 15:13:25 UTC, jmh530 wrote:
> I suppose my issue is that final should prevent function 
> hijacking because I shouldn't be allowed to override string 
> bar(double d) anyway. It shouldn't be a worry.

It has nothing to do with overriding. Consider:

import std.stdio;

class A {
     final void foo(int) {
         writeln("A.foo(int)");
     }
}

class B : A {
     final void foo(long) {
         writeln("B.foo(long)");
     }
}

void main() {
     B b = new B;
     int n = 1;
     b.foo(n);
}

That prints "B.foo(long)", even though foo() was called with an 
int (tbh, I'd say it's hijacking and shouldn't even compile, like 
it doesn't with virtual functions - try to remove finals). The 
compiler starts looking from B, finds name "foo" and tries that 
without looking any futher into base classes.

If you want that, you can do it manually:

class Foo2 : Foo
{
     alias bar = super.bar; // bring Foo.bars in scope
     override void bar(int x) { }
}


More information about the Digitalmars-d-learn mailing list