An
operator in
C++ to get type information. This allows a program to do get information about a class at
runtime, and can be considered a very basic form of
reflection (as in
Java).
The typeid operator may be called on any object or class. It returns an object of type type_info, which is declared in the typeinfo header file. type_info objects can be compared for equality and inequality (operator== and operator!=) and can return the name of the class (implementation dependant, but always zero terminated). Additionally, the before method gives the relative comparison order, also implementation dependant.
Example:
class A {
};
A a;
type_info info1 = typeid(A); // get type info for class A
type_info info2 = typeid(a); // get type info for object a
if (info1 == info2) { // type_infos can be tested for equality
// do something
}
// the name of the type can be printed, useful for debugging
cout << info1.name() << ' ' << info2.name() << endl;