Overloading/Inheritance issue

Steven Schveighoffer schveiguy at yahoo.com
Wed Aug 8 08:07:55 PDT 2007


"Walter Bright" <newshound1 at digitalmars.com> wrote in message 
news:f9bcvt$1vqb$1 at digitalmars.com...
> Bill Baxter wrote:
>> So does that mean you can't do non-virtual overloads in D?
>
> Right, you cannot.
>
>> I.e. have a situation where:
>>                 x.foo()
>> and
>>     (cast(Base)x).foo()
>> call different member functions?

This is kind of cheating, but it does show a possibility where the lines of 
code you give do call 2 different functions :)  Of course, it's completely 
off topic, but a fun exercise.

import tango.io.Stdout;

class Base
{
  void foo(char[] x = "blah")
  {
    Stdout("in Base::foo").newline;
  }
}

class Derived : Base
{
  void foo(int x = 5)
  {
    Stdout("in Derived::foo").newline;
  }
}

int main(char[][] args)
{
  Derived x = new Derived;
  x.foo();
  (cast(Base)x).foo();
  return 0;
}

output:
in Derived::foo
in Base::foo

-Steve 





More information about the Digitalmars-d mailing list