typeid — Runtime Type Information (RTTI)The typeid operator provides runtime type information.
It returns a reference to a std::type_info object describing the type of an expression.
typeid(expr) uses the static (compile-time) type.typeid(*ptr) uses the dynamic (runtime) type if the expression is an lvalue to a polymorphic object.#include <iostream>
#include <typeinfo>
struct Base { virtual ~Base() {} };
struct Derived : Base {};
int main() {
Base* p = new Derived;
std::cout << typeid(*p).name(); // "Derived" (dynamic type)
std::cout << typeid(p).name(); // "Base*" (static type)
}
typeid returns a std::type_info object, which supports:
.name()== and !=