Another bug in function overloading?

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Apr 26 03:23:25 PDT 2014


On Sat, 26 Apr 2014 06:55:38 +0000
Domain via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:

> module test;
> 
> public interface I
> {
>      void foo();
>      void foo(int);
> }
> 
> public abstract class A : I
> {
>      public void bar()
>      {
>          foo();
>      }
> 
>      public void foo(int i)
>      {
>      }
> }
> 
> public class C : A
> {
>      public void foo()
>      {
>      }
> 
>      public void bar2()
>      {
>          foo(1);
>      }
> }
> 
> Error: function test.A.foo (int i) is not callable using argument 
> types ()
> Error: function test.C.foo () is not callable using argument 
> types (int)


No. That's expected. If you've overloaded a function from a base class,
only the functions in the derived class are in the overload set, so you
have to bring the base class' overload into the overload by either
overriding the base class overload in the derived class or by aliasing
it in the derived class. e.g.

module test;

public interface I
{
    void foo();
    void foo(int);
}

public abstract class A : I
{
    public void bar()
    {
        foo();
    }

    alias I.foo foo;
    public void foo(int i)
    {
    }
}

public class C : A
{
    alias A.foo foo;
    public void foo()
    {
    }

    public void bar2()
    {
        foo(1);
    }
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list