how to uuse 'typeid'
I wanted to use 'typeid' like I would use 'instanceof' in java. What's the easiest and best way to facilitate this in C++? Also, does typeid( parameter ).name() return a mangled string of some sort, or the actual clear name of the class to use for a means of comparison? Should I just declare an arbitrary object of a certain type, and use it as a means of comparison of my current object? Any help would be great.
In this example, value can be any class in a hiearchy I am working with
if( typeid( value ) == typeid( x ) ) //check if value is of type x (Arbitrary). Or would I have to use .name() for both of them?
if( typeid( value ).name() == "Arbitrary" ) or if( typeid( value ).name() == Arbitrary ) //Check for string comparison, for whichever one is the proper way of coding it
The first one is correct:
if ( typeid(Arbitrary) == typeid(x) )
Would be true in this.
const_info::name returns a "human readable" string for the representation of the type. I don't know if it's the same across compilers so I'd advise against using it for detecting type.
Also note that you can't compare a string like in your second example, because it returns const char*.
The format of the name() return value is not standardized. Some compilers return the mangled name, some the unmangled form, and the only guarantee is that typeid(x) == typeid(y) implies typeid(x).name() == typeid(y).name() and vice versa. (Using a conceptual == - as Elysia said, they're C strings and must be compared with strcmp.)