[Insight-users] determine class name dynamically

Luis Ibanez luis . ibanez at kitware . com
Fri, 07 Nov 2003 10:13:48 -0500


Hi Bing,

The RTTI support in C++ allows you to do
operations like the following:


    #include <typeinfo>

    myClass * myObject;
    ClassA  * classA;


    if( typeid( myObject ) == typeid( classA ) )
       {
        //...
       }


    if( typeid( myObject ) != typeid( classA ) )
       {
       //....
       }


Note that for RTTI to work correctly for polymorphism,
the argument passed to "typeid()" should be a pointer
or a reference to an object.

--

You can also print out a string identification
of the types by using

    std::cout <<  typeid( myObject ).name()   << std::endl;


Note however that the resulting string is
compiler dependent. That is, the string id
produced by Visual C++ is not the same that
the one produced by GCC. So this is mostly useful
for human users for debugging purposes.

For example, the Print() method in ITK objects
include the typeid().name() of the class being
printed.



Regards,


    Luis


-------------------
Bing Jian wrote:
> Hi,
> 
> I'd like to know how to know the object's class
> dynamically in ITK. For example, I need to do
> something like
> 
>   if (classname(m_Memeber) is A)
>    { ... }
>   else
>    { ... }
> 
> 
>   Maybe this is an issue only concerned with C++.
> 
>   Thanks!
>