polymorphism: overloading vs. overriding

Markus Kranz Markus_member at pathlink.com
Sun May 7 02:22:08 PDT 2006


Following the rule of last surprise: wouldn't it be desirable if the output of
the following program would be:

Bar.doIt()
Bar.doIt(Bar)

The actual output

Bar.doIt()
Foo.doIt(Foo)

at least for me is a little bit surprising since the best matching function
seems to be Bar.doIt(Bar).
As overriding with covariant return types is supported it seems a bit unnatural
to me that overriding with covariant arguments does not work.

Instead of doing overload resolution before virtual function resolution (what
seems to be done now) an implementation of 'covariant arguments' theoretically
could be as simple as reversing the order of resolution.

What do you think?

Regards,
Markus

--
import std.stdio;

class Foo {
void doIt() { writefln("Foo.doIt()"); }
void doIt(Foo that) { writefln("Foo.doIt(Foo)"); }
}

class Bar : Foo {
//overrides Foo.doIt()
void doIt() { writefln("Bar.doIt()"); }

//overloads (inherited) Foo.doIt(Foo)
void doIt(Bar that) { writefln("Bar.doIt(Bar)"); }
}

int main() {
Foo foo = new Bar();

foo.doIt();
foo.doIt(foo);
}





More information about the Digitalmars-d mailing list