interface problem

Ary Borenszweig ary at esperanto.org.ar
Mon Mar 8 06:34:32 PST 2010


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.


More information about the Digitalmars-d-learn mailing list