How should overruling of overloaded functions work?

Bruno Medeiros brunodomedeiros+spam at com.gmail
Tue Aug 22 07:15:56 PDT 2006


Kristian wrote:
> On Sat, 19 Aug 2006 18:09:25 +0300, Kristian <kjkilpi at gmail.com> wrote:
>> 3)
>> If the overloading does not work for derived classes, then the 
>> following common case does not work without the alias hack:
>>
>> class String {...}
>>
>> class Base {
>>      void f(String s) {...}
>>      //these functions are provided for convenience...
>>      final void f(int v) {
>>          f(new String(v));
>>          }
>>      final void f(float v) {
>>          f(new String(v));
>>          }
>> }
>>
>> class Derived : Base {
>>      void f(String s) {...}  //overrule the main function that does 
>> all the work
>> }
> 
> 
> Okey, okey, why doesn't someone tell me that this actually works!? :)
> 
> The functions 'f(int)' and 'f(float)' are not hidden from Derived 
> because they're final. Nice!
> 
> This was the main reason I protested against the current overload 
> function hiding. But because it do work, I will fell silent (and look a 
> bit embarrassed). ;)
> 
> This enables me to write these convenience functions without the need of 
> rewriting (or alias hacking) them in subclasses. Non-final functions 
> should be reimplemented anyway (if someone wants to use alias hacking 
> for those, be my guest).

Are you sure it works? It's not working for me, I tried the following code:

-----
import std.stdio;

class String {}

class Base {
     void f(String s) {
     	writefln("Base.f(String)");
     }
     //these functions are provided for convenience...
     final void f(int v) {
         writefln("Base.f(int)");
     }
     final void f(float v) {
         writefln("Base.f(float)");
     }
}

class Derived : Base {
     void f(String s) { writefln("Derived.f"); }
}

void test() {
     Derived obj = new Derived;
     obj.f(10);  // ERROR, doesn't work
}




More information about the Digitalmars-d mailing list