typeid of an object whose static type is an interface returns the interface

Kapps via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 28 22:07:11 PDT 2014


On Friday, 27 June 2014 at 21:23:52 UTC, Mark Isaacson wrote:
> If I have a variable whose static type is an interface and I 
> call
> typeid on it, I get the interface back, not the dynamic type.
> This seems like confusing behavior. Is this the intended result?
>
> I recognize that one needs some amount of state to perform the
> dynamic type lookup, and so it is on that thought that a reason
> for this might be based.

Interfaces are not necessarily Objects (particularly with the 
case of IUnkown or extern(C++)), and are handled somewhat 
differently from objects.

When you cast to Object, you're actually subtracting a few bytes 
from the pointer to get back to the Object, so technically the 
variable refers not to Object but to the interface. It is a bit 
odd (along with some of the other side-effects), but it does make 
some sense since you're referring to the interface and not to an 
Object.

That being said, I'm not 100% sure whether this is the intended 
behaviour when you actually do point to a class derived from 
Object.


More information about the Digitalmars-d mailing list