Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 06, 2010 interface problem | ||||
---|---|---|---|---|
| ||||
I got 2 classes which both (indirectly) inherit from a common base class and implement a certain interface I. Now I need to pass that interface to a function and need to call a function inherited from the base class inside. Naturally that function is not present in the interface and thus can't be called, so how to solve that problem? Adding the function to the interface doesn't work. It doesn't recognize the inherited version and yields "not implemented" |
March 06, 2010 Re: interface problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | Trass3r: > I got 2 classes which both (indirectly) inherit from a common base class and implement a certain interface I. When possible show an example. You can fill/fix the following code to create an example of your problem: interface IFoo { void foo(); } class Base { void foo() {} } class C1 : Base, IFoo { void foo1() {} } class C2 : Base, IFoo { void foo1() {} } void bar() { } void main() { } > Adding the function to the interface doesn't work. It doesn't recognize the inherited version and yields "not implemented" This is a very new feature, so it can be a little buggy. Bye, bearophile |
March 06, 2010 Re: interface problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | On 3/6/10 13:39, Trass3r wrote: > I got 2 classes which both (indirectly) inherit from a common base class and implement a certain interface I. > > Now I need to pass that interface to a function and need to call a function inherited from the base class inside. > Naturally that function is not present in the interface and thus can't be called, so how to solve that problem? Cast the instance to the base class and call the method? > Adding the function to the interface doesn't work. It doesn't recognize the inherited version and yields "not implemented" |
March 06, 2010 Re: interface problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | > Cast the instance to the base class and call the method?
>
Surprisingly this does compile.
What are the rules for casting between classes?
|
March 06, 2010 Re: interface problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r Attachments: | Trass3r wrote: >> Cast the instance to the base class and call the method? >> > > Surprisingly this does compile. > What are the rules for casting between classes? I don't think there are any rules as such. Casting in D is not well specified. Between classes you'll probably get a dynamic cast which happens at run time and a dynamic cast can return a null reference, so you'll get a crash if you don't guard against it. You can force a reinterpret cast by casting to void* and then casting again, but you have to be careful doing that as you are deliberately subverting type checking. - -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk |
March 08, 2010 Re: interface problem | ||||
---|---|---|---|---|
| ||||
Posted in reply to Trass3r | Trass3r wrote:
>> Cast the instance to the base class and call the method?
>>
>
> Surprisingly this does compile.
> What are the rules for casting between classes?
Normally you can cast A to B if B inherits from A. But this seems not to be the case in D. The following compiles without complains:
import std.stdio;
interface I {
}
class A : I {
}
class B {
}
int main() {
I i = new A();
A a = cast(A) i;
B b = cast(B) i; // shouldn't compile
B c = cast(B) a; // shouldn't compile
writeln(a);
writeln(b);
writeln(c);
return 0;
}
And prints:
main.A
null
null
I'll move this to another thread.
|
Copyright © 1999-2021 by the D Language Foundation