www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - interface problem

reply Trass3r <un known.com> writes:
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"
Mar 06 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
Mar 06 2010
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
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"

Mar 06 2010
parent reply Trass3r <un known.com> writes:
 Cast the instance to the base class and call the method?
 

Surprisingly this does compile. What are the rules for casting between classes?
Mar 06 2010
next sibling parent div0 <div0 users.sourceforge.net> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iD8DBQFLkqbKT9LetA9XoXwRAiTtAJ9STEAd+nQxf9URIV+X9HTwLr6TJACfaGoi fTrzOMyeQNbH95gsj+4sNTU= =eYEH -----END PGP SIGNATURE-----
Mar 06 2010
prev sibling parent Ary Borenszweig <ary esperanto.org.ar> writes:
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.
Mar 08 2010