Checking runtime object type
Justin Whear
justin at economicmodeling.com
Wed Feb 8 11:22:09 PST 2012
On Wed, 08 Feb 2012 11:20:39 -0800, H. S. Teoh wrote:
> What's the correct syntax for checking the runtime type of a derived
> object given its base class pointer? I tried:
>
> Base f() { return new Derived(); }
> Base b = f();
> assert(is(typeof(b)==Derived));
>
> but it throws an error. Apparently typeof(b)==Base; so typeof returns
> only compile-time information? How do I get at the runtime type?
>
>
> T
Yeah, is() and typeof() are purely compile-time. You'll want the typeid
expression (http://d-programming-language.org/
expression.html#typeidexpression) which returns an instance of TypeInfo
(http://d-programming-language.org/phobos/object.html#TypeInfo).
More information about the Digitalmars-d-learn
mailing list