Override with function overloads

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Sep 10 21:29:39 PDT 2017


On 09/10/2017 08:14 PM, jmh530 wrote:
 > In the code below, the second to the last line fails to compile. As far
 > as I can tell it is because the override also overrides all overloads. I
 > could imagine why it would occur with a virtual member function, but I
 > would think it wouldn't be necessary with a final one.
 >
 > @system
 > unittest
 > {
 >     class Foo
 >     {
 >         final string bar(double d) { return ""; }
 >         void bar(int x) { x++; };
 >     }
 >
 >     class Foo2 : Foo
 >     {
 >         override void bar(int x) { }
 >     }
 >
 >     Foo2 foo2 = new Foo2;
 >     foo2.bar(1);
 >     string x = foo2.bar(1.0); //error that bar is not callable with 
doubles
 >     //string x = foo2.Foo.bar(1.0); //this compiles
 > }

Here, the feature called "name hiding" is in effect. Foo2.bar hides all 
bars from Foo. This is to avoid "function hijacking"[1].

Ali

[1] https://dlang.org/hijack.html



More information about the Digitalmars-d-learn mailing list