interface not able to use functions from base classes??
Todd VanderVeen
tdvanderveen at gmail.com
Wed Dec 25 00:55:52 PST 2013
I presume your intent is to have B inherit the implementation
from A. There are two options.
If you control class A and its appropriate to declare the
interface there, you can declare that A implements I and have B
simply inherit it from A.
class A : I {
int f(int x) { return x; }
}
interface I { int f(int x); }
class B : A { }
void main() {
writeln(new B().f(7));
}
If you only intend to introduce the interface on B, you still
need to provide an implementation for I, either voluntarily
overriding A or providing an alternate implementation to the
virtual interface. To do the former, you can do the following:
class A {
int f(int x) { return x; }
}
interface I { int f(int x); }
class B : A, I {
override int f(int x) { return super.f(x); }
}
void main() {
writeln(new B().f(7));
}
On Wednesday, 25 December 2013 at 07:40:03 UTC, Øivind wrote:
> Why doesn't this work:
>
> ----
>
> class A {
> int f(int x) {
> return x;
> }
> }
>
> interface I {
> int f(int x);
> }
>
> class B : A, I {
>
> }
>
> void main() {}
>
> ----
>
> I get the following error with DMD 2.064.2:
>
> /d122/f338.d(11): Error: class f338.B interface function 'int
> f(int x)' is not implemented
More information about the Digitalmars-d
mailing list