MC++ FAQ
Monday 07 | 22 | 2002
Testing Type Equivalence
C# has both is and as operators to do type convertions (casting) and type equivalence checks (that is, see if a given object can be safely converted to another type inside an class or interface hierarchy). MC++, on the other hand, has no direct equivalence.

MC++, instead, has dynamic_cast and __try_cast. In other words, the way to mimic the behavior of C#'s operators is to try the cast and see if it fails.

The preffered alternative is, of course, to use dynamic_cast, since it returns NULL if the conversion cannot be done. This makes type-testing very easy. For example, mimicking C#'s is operator in MC++ can be done with a simple template:

template <typename T1, typename T2>
inline bool istypeof ( T2* t )
{
   return (dynamic_cast<T1*>(t) != 0);
}

So now you can do something like this:

if ( istypeof<System::String>(object) ) {
}

Mimicking C#'s as operator, on the other hand, is essentially unnecessary, since, as you can see, the way it works is just how dynamic_cast behaves in MC++.

Posted by tomasr at July 22, 2002 04:04 AM
Comments
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?

Copyright(C) 2002, Tomas Restrepo, All rights reserved. Powered by Movable Type 2.63