RTTI stands for RunTime Type Identification. Commonly two ways to achieve it.
Dynamic cast
class Base {};
class Derived : public Base {};
void foo(Base* b)
{
Derived* d = dynamic_cast<Derived*>(b);
if (d != nullptr)
{
// b is actually points to a Derived object.
}
}
Typeid
typeid
is a operator in c++, which gives the type_info
struct to the given type. See more in cppreference.
Summary
Dynamic cast needs to work in polymorphic. Once use RTTI, every type will contain a extra info struct type_info
.
Pass the -fno-rtti
to the compiler to disable this, i.e., RTTI incurs extra cost.
See also 继承.