digitalmars.D.learn - interface problem
- Trass3r (4/4) Mar 06 2010 I got 2 classes which both (indirectly) inherit from a common base class...
- bearophile (21/23) Mar 06 2010 When possible show an example. You can fill/fix the following code to cr...
- Jacob Carlborg (2/6) Mar 06 2010
- Trass3r (2/4) Mar 06 2010 Surprisingly this does compile.
- div0 (22/27) Mar 06 2010 -----BEGIN PGP SIGNED MESSAGE-----
- Ary Borenszweig (25/30) Mar 08 2010 Normally you can cast A to B if B inherits from A. But this seems not to...
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
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
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
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
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Trass3r wrote: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-----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
Trass3r wrote: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.Cast the instance to the base class and call the method?Surprisingly this does compile. What are the rules for casting between classes?
Mar 08 2010