interface and class inheritance

Ali Çehreli acehreli at yahoo.com
Thu Nov 14 13:40:16 PST 2013


On 11/14/2013 01:20 PM, Oleg B wrote:
> [code]
> import std.stdio;
>
> interface A { void funcA(); }
> class B { final void funcA() { writeln( "B.funcA()" ); } }
>
> class C: B, A { }
>
> void main()
> {
>      auto c = new C;
>      c.funcA();
> }
> [code/]
>
> $ dmd -run interface.d
> interface.d(6): Error: class interface.C interface function 'void
> funcA()' is not implemented
>
> if swap A and B
> [code]
> class C: A, B { }
> [code/]
>
> $ dmd -run interface.d
> interface.d(6): Error: class interface.C base type must be interface,
> not interface.B
>
> how to workaround this without change in class B and interface A?

One way is to expose B through 'alias this' (not by inheritance) but it 
doesn't scale because currently only one 'alias this' is allowed.

import std.stdio;

interface A { void funcA(); }
class B { final void funcA() { writeln( "B.funcA()" ); } }

class C: A {
     B b;
     alias b this;

     this(){
         b = new B();
     }

     void funcA()
     {
         return b.funcA();
     }
}

void main()
{
     auto c = new C;
     c.funcA();
}

Ali



More information about the Digitalmars-d-learn mailing list