Override with function overloads

jmh530 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 11 11:15:36 PDT 2017


On Monday, 11 September 2017 at 17:59:25 UTC, nkm1 wrote:
> 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:
> [snip]

An interesting example. I'm not sure overriding is the issue so 
most as what is in the overload set. I think foo(int) is not part 
of the overload set yet. The compiler is able to cast the long to 
int and then call the one in class B without needing to look to 
the base class. The behavior is also the same if you use alias 
this (below).

Would there be any problems with final functions of inherited or 
alias this types being included in the overload set?



import std.stdio;

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

class B {
     A a = new A;
     alias a this;

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

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


More information about the Digitalmars-d-learn mailing list