βš™οΈ C++ Advanced Concepts
Estimated reading: 3 minutes 337 views

C++ RTTI (Run-Time Type Information) – Identify Object Types at Runtime


Introduction – Why Use RTTI in C++

C++ is a statically-typed language, but in polymorphic scenarios, it’s sometimes essential to determine the actual type of an object at runtime. This is where RTTI (Run-Time Type Information) comes in. It helps in safe typecasting, type checks, and debugging without breaking type safety.

In this guide, you’ll learn:

  • What RTTI is and how it works
  • How to use typeid and dynamic_cast
  • Real-world examples of RTTI
  • Best practices and limitations

What Is RTTI?

RTTI (Run-Time Type Information) allows C++ programs to determine object types during execution. It works only with polymorphic classes (classes with at least one virtual function) and is enabled by default unless explicitly disabled.


Key RTTI Operators

OperatorDescription
typeidReturns the actual type of an expression/object
dynamic_castSafely casts pointers/references in class hierarchies

Code Examples – With Output

Example 1: Using typeid for Type Identification

#include <iostream>
#include <typeinfo>
using namespace std;

class Animal {
public:
    virtual ~Animal() {}
};

class Dog : public Animal {};

int main() {
    Animal* a = new Dog();
    cout << typeid(*a).name() << endl;  // Output: class Dog (implementation-defined)
    delete a;
}

Output: Implementation-defined name like Dog or class Dog


Example 2: Safe Downcasting with dynamic_cast

Animal* a = new Dog();
Dog* d = dynamic_cast<Dog*>(a);

if (d) {
    cout << "Downcast successful" << endl;
} else {
    cout << "Invalid cast" << endl;
}

If cast fails, dynamic_cast returns nullptr for pointers or throws bad_cast for references.


🚨 dynamic_cast vs static_cast

Featuredynamic_caststatic_cast
Runtime check Yes No
Polymorphic req Yes (base class must be virtual) No
Safe casting Risk of undefined behavior

When to Use RTTI

  • Type-checking in polymorphic object hierarchies
  • Safe downcasting in class hierarchies
  • Logging and debugging object types
  • Implementing serialization, plugin systems, interpreters

Best Practices & Tips

Always ensure base class has a virtual destructor to enable RTTI
Use dynamic_cast only when necessaryβ€”prefer virtual function overrides where possible
Avoid RTTI in performance-critical code (may introduce overhead)


Use Cases for RTTI

Plugin Systems: Detect and cast plugin types dynamically
Game Engines: Downcast base Entity to specific subclasses like Player or Enemy
Debugging Tools: Log types during testing or fault tracing
File Parsers: Handle polymorphic file sections or formats safely


Summary – Recap & Next Steps

Key Takeaways:

  • RTTI enables type inspection and safe downcasting at runtime
  • Use typeid to get the real type; dynamic_cast to convert safely between base and derived pointers
  • Only works with polymorphic types

Real-World Relevance:
RTTI is crucial in extensible systems, type inspection tools, complex class hierarchies, and polymorphic containers.


FAQ – C++ RTTI

What is RTTI used for?
For identifying and safely casting object types at runtime in polymorphic hierarchies.

Is RTTI enabled by default?
Yes, unless explicitly disabled by compiler flags.

What happens if dynamic_cast fails?
Returns nullptr for pointers, throws bad_cast for references.

Can I use RTTI with non-polymorphic classes?
No. The base class must have at least one virtual function.

Does typeid work with references and pointers?
Yes. Use typeid(*ptr) to get the dynamic type, not the static one.


Share Now :
Share

C++ RTTI (Run-Time Type Info)

Or Copy Link

CONTENTS
Scroll to Top