Casting from interface to real class?

Bill Baxter dnewsgroup at billbaxter.com
Sun Jan 20 21:26:01 PST 2008


Bill Baxter wrote:
> Is it possible to do a dynamic cast from an interface to a real class?
> 
> SomeClass k = cast(SomeClass)anInterface;
> 
> will that succeed if anInterface started life as a SomeClass?

I think I found the answer: Yes.

http://spottedtiger.tripod.com/D_Language/D_FAQ_XP.html

"""
Casting from an interface back to the class means subtracting that 
offset. The subtract is a little tricky since the pointer doesn't know 
where it came from, so the first entry in the interface's vtbl[] is an 
instance of the class Interface with the necessary offset. The cast is 
handled by the library function _d_interface_cast().
"""

--bb


More information about the Digitalmars-d-learn mailing list