interface and class inheritance

Stretto uiy12345 at gmail.com
Sun Nov 17 08:41:20 PST 2013


On Thursday, 14 November 2013 at 21:20:57 UTC, 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?

This is ambiguous. In one case, from A, you are allowing 
inheritance and in B you are preventing it.

c.funcA will call the interface version unless you cast to B.

But it should be an easy fix

class C : B, A
{
     void funcA() { ((cast(B))this).funcA(); }
}

This simply redirects funcA(in C, but using interface A) to funcA 
in B, which is final.


More information about the Digitalmars-d-learn mailing list