digitalmars.D.learn - Checking runtime object type
- H. S. Teoh (10/10) Feb 08 2012 What's the correct syntax for checking the runtime type of a derived
- Johannes Pfau (7/20) Feb 08 2012 I think using casts is the only way:
- Jonathan M Davis (6/28) Feb 08 2012 Casting is definitely the way that you're supposed to do it. If the cast...
- Steven Schveighoffer (8/36) Feb 08 2012 It depends on the usage. If you want to see what the most derived type ...
- Justin Whear (5/17) Feb 08 2012 Yeah, is() and typeof() are purely compile-time. You'll want the typeid
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 -- "You are a very disagreeable person." "NO."
Feb 08 2012
Am Wed, 8 Feb 2012 11:20:39 -0800 schrieb "H. S. Teoh" <hsteoh quickfur.ath.cx>: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? TI think using casts is the only way: Base f() { return new Derived(); } Base b = f(); auto c = cast(Derived)b; assert(c !is null);
Feb 08 2012
On Wednesday, February 08, 2012 20:21:45 Johannes Pfau wrote:Am Wed, 8 Feb 2012 11:20:39 -0800 schrieb "H. S. Teoh" <hsteoh quickfur.ath.cx>:Casting is definitely the way that you're supposed to do it. If the cast results in null, then the class is _not_ of the type that you cast to. e.g. if(auto d = cast(Derived) b) //do stuff with d - Jonathan M DavisWhat'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? TI think using casts is the only way: Base f() { return new Derived(); } Base b = f(); auto c = cast(Derived)b; assert(c !is null);
Feb 08 2012
On Wed, 08 Feb 2012 14:41:51 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote:On Wednesday, February 08, 2012 20:21:45 Johannes Pfau wrote:It depends on the usage. If you want to see what the most derived type is, using typeid is best (for those old-schoolers, this used to be .classinfo). If you want to *verify* that the given type is derived from some other type, using cast is best. -SteveAm Wed, 8 Feb 2012 11:20:39 -0800 schrieb "H. S. Teoh" <hsteoh quickfur.ath.cx>:Casting is definitely the way that you're supposed to do it. If the cast results in null, then the class is _not_ of the type that you cast to. e.g. if(auto d = cast(Derived) b) //do stuff with dWhat'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? TI think using casts is the only way: Base f() { return new Derived(); } Base b = f(); auto c = cast(Derived)b; assert(c !is null);
Feb 08 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? TYeah, 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).
Feb 08 2012