typeid
From Wikipedia, the free encyclopedia
In C++, the typeid keyword is used to determine the class of an object at runtime. According to the C++ specification, it returns a reference to type_info. The use of typeid is often preferred over dynamic_cast<class_type> in situations where just the class information is needed, because typeid is a constant-time procedure, whereas dynamic_cast must traverse the class derivation lattice of its argument at runtime.
[edit] Example
#include <iostream> #include <typeinfo> using namespace std; class Person { public: // ... Person members ... virtual ~Person() {} }; class Employee : public Person { // ... Employee members ... }; int main () { Person person; Employee employee; Person *ptr = &employee; cout << typeid(person).name() << endl; cout << typeid(employee).name() << endl; cout << typeid(ptr).name() << endl; cout << typeid(*ptr).name() << endl; return 0; }
Output (exact output varies by system):
Person Employee *Person Employee

