Casting pointers

Henning Hasemann hhasemann at web.de
Sat Jul 7 07:16:38 PDT 2007


I have the following code:

import std.stdio;

interface I { void foo(); }

class C : I {
  void foo() { writefln("foo called"); }
}

void main() {
  auto c = new C;
  I* i = cast(I*)(&c);
  (*i).foo();
}

What I'd expect it to do would be to write "foo called".
Instead it prints out "icast.C" with dmd-1.018.

Did I something wrong casting the address of c (which is in fact a
reference to a reference)? If so shouldnt there be a compiler error?

Is there any way to have the address of the Variable c interpreted as
an address to a I-interface at all? I know it works without the address
stuff but I'd really like to use this pointer-to-reference semantic.

Henning

-- 
GPG Public Key:
http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851
Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851



More information about the Digitalmars-d mailing list