typeid
In C++, the typeid keyword is used to determine the class of an object at run time. It returns a reference to std::type_info object, which exists until the end of the program[1]. 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. Some aspects of the returned object are implementation-defined, such as std::type_info::name(), and cannot be relied on across compilers to be consistent.
Objects of class std::bad_typeid are thrown when the expression for typeid is the result of applying the unary * operator on a null pointer. In other words, the expression must take the form typeid(*p) where p is any expression resulting in a null pointer.
[edit] Example
#include <iostream> #include <typeinfo> //for 'typeid' class Person { public: // ... Person members ... virtual ~Person() {} }; class Employee : public Person { // ... Employee members ... }; int main() { Person person; Employee employee; Person *ptr = &employee; Person &ref = employee; // The string returned by typeid::name is implementation-defined std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time) std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time) std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time) std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time // because it is the dereference of a // pointer to a polymorphic class) std::cout << typeid(ref).name() << std::endl; // Employee (references can also be polymorphic) Person* p = 0; try { typeid(*p); // not undefined behavior; throws std::bad_typeid // *p, *(p), *((p)), etc. all behave identically } catch (...) {} Person& pRef = *p; // undefined behavior, dereferences null typeid(pRef); // does not meet requirements to throw std::bad_typeid // because it the expression for typeid is not the result // of applying the unary * operator; behavior is undefined }
Output (exact output varies by system):
Person Employee Person* Employee Employee
[edit] See also
[edit] References
- ^ C++ standard (ISO/IEC14882) section 5.2.8 [expr.typeid], 18.5.1 [lib.type.info] -- www-d0.fnal.gov/~dladams/cxx_standard.pdf