C++ RTTI (Runtime Type Information)

beginner
13 min

C++ RTTI (Runtime Type Information)

Welcome to our comprehensive guide on C++ RTTI (Runtime Type Information)! This lesson is designed to help you understand the powerful concept of RTTI in a friendly and practical manner.

Let's start with the basics!

What is RTTI in C++?

šŸ’” Pro Tip: RTTI stands for Runtime Type Information, a feature in C++ that allows you to determine the exact type of an object at runtime. This is useful when dealing with polymorphic objects, as it enables us to access specific member functions or data unique to each object's type.

Why do we need RTTI?

In C++, polymorphism allows objects of different classes to be treated as instances of a common base class. However, without RTTI, we cannot know the exact type of the object at runtime. RTTI solves this problem by providing a way to query the actual type of an object at runtime.

How to use RTTI in C++?

To use RTTI in C++, you'll need to understand three key concepts:

  1. dynamic_cast: This operator allows you to perform a runtime type check and cast between related classes.

  2. typeid: This operator returns an object of type std::type_info, which represents the type of an object at runtime.

  3. typeid_info::name(): This function returns a null-terminated string representing the name of the type.

Practical Example 1: Polymorphic Base and Derived Classes

Let's create a simple example using base and derived classes:

cpp
#include <iostream> #include <typeinfo> class Base { public: virtual void print() { std::cout << "Base" << std::endl; } }; class Derived : public Base { public: void print() { std::cout << "Derived" << std::endl; } }; int main() { Base *base = new Derived(); Base &ref = *base; base->print(); // Output: Derived (Polymorphism in action!) std::cout << typeid(*base).name() << std::endl; // Output: _ZN5DerivedE // Using dynamic_cast Derived *derived = dynamic_cast<Derived*>(base); if (derived != nullptr) { derived->print(); // Output: Derived } return 0; }

In this example, we have a base class Base and a derived class Derived. We create an instance of Derived and store it in a base class pointer base. Using polymorphism, when we call the print() function, it executes the appropriate function for the actual type of the object.

We also demonstrate the use of typeid and dynamic_cast to determine the actual type of the object at runtime.

Practical Example 2: RTTI with Polymorphic Function Calls

Now, let's create another example involving polymorphic function calls:

cpp
#include <iostream> #include <typeinfo> class Shape { public: virtual void print() { std::cout << "Unknown shape" << std::endl; } }; class Circle : public Shape { public: Circle(double radius) : radius_(radius) {} void print() override { std::cout << "Circle with radius: " << radius_ << std::endl; } double radius() const { return radius_; } private: double radius_; }; class Square : public Shape { public: Square(double side) : side_(side) {} void print() override { std::cout << "Square with side: " << side_ << std::endl; } double side() const { return side_; } private: double side_; }; int main() { Shape *shapes[] = { new Circle(5.0), new Square(4.0) }; for (Shape *shape : shapes) { shape->print(); std::cout << typeid(*shape).name() << std::endl; } return 0; }

In this example, we have a base class Shape and derived classes Circle and Square. We create an array of pointers to Shape and store instances of both derived classes.

When we iterate through the array and call the print() function, it executes the appropriate function for each object's type. We also demonstrate the use of typeid to determine the actual type of each object at runtime.

Quiz

Quick Quiz
Question 1 of 1

What does RTTI stand for in C++?

Wrapping Up

Congratulations on mastering the basics of C++ RTTI! In this lesson, we covered the importance of RTTI, demonstrated its usage through practical examples, and tested your knowledge with a quiz.

Remember, the key to understanding RTTI is to practice! Try implementing RTTI in your own projects and explore more about C++'s powerful polymorphic capabilities.

Keep learning, and happy coding! šŸŽ‰šŸŽÆšŸ’”šŸ“