digitalmars.D.learn - How to check type of an object to a class name?
- Chris Pons (7/7) Mar 05 2012 Is it possible to check the type of an object to a class name?
- Pedro Lacerda (4/11) Mar 05 2012 This works:
- Jonathan M Davis (21/30) Mar 05 2012 If you want to check whether a particular object is of a particular type...
Is it possible to check the type of an object to a class name? //Something like this: Class test { //... } assert(is(anonObject == typeof(test))
Mar 05 2012
This works: assert(typeid(anonObject) == typeid(test)); Pedro Lacerda 2012/3/5 Chris Pons <cmpons gmail.com>Is it possible to check the type of an object to a class name? //Something like this: Class test { //... } assert(is(anonObject == typeof(test))
Mar 05 2012
On Tuesday, March 06, 2012 01:37:05 Chris Pons wrote:Is it possible to check the type of an object to a class name? //Something like this: Class test { //... } assert(is(anonObject == typeof(test))If you want to check whether a particular object is of a particular type at runtime, then cast and check whether it's null. If you want to know the _exact_ type of an object, then use typeid. class A {} class B : A {} class C : B {} void main() { A obj = new C; assert(cast(A)obj); assert(cast(B)obj); assert(cast(C)obj); assert(typeid(obj) != typeid(A)); assert(typeid(obj) != typeid(B)); assert(typeid(obj) == typeid(C)); } - Jonathan M Davis
Mar 05 2012