get actual classinfo through an interface?
Christopher Wright
dhasenan at gmail.com
Wed Nov 21 12:54:00 PST 2007
BC wrote:
> new question. since IInterface.classinfo returns IInterface, how to get
> the actual value of the concrete class?
>
Do you have a variable that's passed as IInterface?
---
interface IFoo {}
class Foo : IFoo {}
void whatever (IFoo foo) {
assert (foo.classinfo is Foo.classinfo);
}
---
That should, I think, work. It fails.
I believe that .classinfo is a static field, which is why it fails. It
needs to be accessed in a virtual way. Since typeof() is compile-time,
it doesn't help.
You can do the ultra-cruddy way, if you're just looking for equality
with some known type, which is naturally completely untested:
bool isType!(T)(Object o) {
auto vtbl = (*(cast(void*[]*)&o))[0];
return vtbl == T.classinfo.vtbl.ptr;
}
If you have a classinfo dictionary, that doesn't help, unless you start
storing by vtbl ptr instead.
Of course, Walter's free to change the layout of objects any time he
wishes, so this is quite version-specific.
More information about the Digitalmars-d-learn
mailing list