digitalmars.D.learn - How to check type of an object to a class name?
- "Chris Pons" <cmpons gmail.com> Mar 05 2012
- Pedro Lacerda <pslacerda gmail.com> Mar 05 2012
- "Jonathan M Davis" <jmdavisProg gmx.com> Mar 05 2012
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
--0015175d03b291178304ba885749 Content-Type: text/plain; charset=UTF-8 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))
--0015175d03b291178304ba885749 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div>This works:</div><div><br></div>assert(typeid(anonObject) =3D=3D typei= d(test));<br clear=3D"all"><div><br></div>Pedro Lacerda<br><br> <br><br><div class=3D"gmail_quote">2012/3/5 Chris Pons <span dir=3D"ltr">&l= t;<a href=3D"mailto:cmpons gmail.com" target=3D"_blank">cmpons gmail.com</a=></span><br><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8e=
Is it possible to check the type of an object to a class name?<br> <br> //Something like this:<br> Class test<br> {<br> //...<br> }<br> <br> assert(is(anonObject =3D=3D typeof(test))<br> </blockquote></div><br> --0015175d03b291178304ba885749--
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









Pedro Lacerda <pslacerda gmail.com> 