Table of contents
1.
Introduction
2.
What is RTTI?
3.
Runtime Casts
3.1.
Explanation:
3.1.1.
Example Code:
3.1.2.
Output
3.2.
Explanation: 
3.2.1.
Example Code:
3.2.2.
Output
4.
Frequently Asked Questions
4.1.
What is a virtual function?
4.2.
What is inheritance?
4.3.
Do virtual functions have to be overridden?
5.
Conclusion
Last Updated: Mar 27, 2024

Run-Time Type Information (RTTI)

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

This blog will explain what RTTI (Runtime Type Information) is in C++. The RTTI mechanism in C++ is a mechanism that exposes information about an object's data type at runtime. This functionality is only available if the class has at least one virtual function. It enables the software to determine the type of an item while it is running.

What is RTTI?

RTTI is an abbreviation for Runtime type identification. It is a way for dynamically determining the type of an object given an accessible pointer or reference to the base type. This is very beneficial when we don't want to rely on the virtual function mechanism's type identification. Virtual functions are commonly used in class libraries to generate runtime type information. Because knowing the runtime type was critical in dealing with exceptions, this functionality was added to C++ with the advent of exception handling. Separate from what is available with the virtual function approach, the RTTI gives an explicit means to specify the runtime type.

Runtime Casts

The runtime cast, which validates the cast, is the simplest method for determining the runtime type of an object with the help of a pointer or reference. This is useful when casting a pointer from a base class to a derived type. Casting an object is generally necessary when working with the inheritance structure of classes.

Casting is classified into two types:

  • Upcasting occurs when a derived class object's pointer or reference is viewed as a base class pointer.
  • Downcasting is the conversion of a base class pointer or reference to a derived class pointer.

Dynamic cast is used in an inheritance hierarchy to downcast a base class reference to a child class. It returns a pointer of the converted type on successful casting, but it fails if we try to cast an invalid type, such as an object pointer that is not of the type of the intended subclass.

Explanation:

Now let's understand the concept of RTTI with the help of an example.

The first code in the following example will not work. It will throw an error similar to "cannot dynamic cast base ptr (of type Base*) to type 'class Derived*' (Source type is not polymorphic)". This error is due to the absence of the virtual function in the example.

Example Code:

#include<iostream>
using namespace std;
class Base { };
class Derived: public Base {};
int main() {
   Base *base_ptr = new Derived;
   Derived *derived_ptr = dynamic_cast<Derived*>(base_ptr);
   if(derived_ptr != NULL)
      cout<<"The code is working";
   else
      cout<<"cannot cast Base* to Derived*";
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

error: cannot dynamic_cast 'base_ptr' (of type 'class Base*') to type 'class Derived*' (source type is not polymorphic)
    7 | Derived *derived_ptr = dynamic_cast<Derived*>(base_ptr);
You can also try this code with Online C++ Compiler
Run Code

 

Explanation: 

A virtual function is a member function that is declared in a base class redefined (overridden) by a derived class. When we use a pointer or a reference to the base class to refer to a derived class object, we may call a virtual function for that object and run the derived class's version of the function.

After adding a virtual method to the base class, it will work. 


Example Code:

#include<iostream>
using namespace std;
class Base {
   virtual void function() {
      //empty function
   }
};
class Derived: public Base {};
int main() {
   Base *base_ptr = new Derived;
   Derived *derived_ptr = dynamic_cast<Derived*>(base_ptr);
   if(derived_ptr != NULL)
      cout<<"The code is working";
   else
      cout<<"cannot cast Base* to Derived*";
   return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Output

The code is working
You can also try this code with Online C++ Compiler
Run Code

 

Check out this article - Compile Time Polymorphism

Know What is Object in OOPs here in detail.

Frequently Asked Questions

What is a virtual function?

When declared in the base class, a virtual function is a method that does not have a definition. This method is left blank in the parent class, and it is redefined in the child class.

What is inheritance?

Inheritance is an OOPs feature that allows classes to inherit common attributes from other classes. For example, if we have a class named vehicle,' additional classes like 'car,' 'bike,' and so on can inherit common attributes. This attribute allows you to remove extra code, lowering the total size of the code.

Do virtual functions have to be overridden?

A pure virtual function must be overridden in a derived class but does not need to be specified.

Conclusion

In this article, we have extensively discussed Runtime type information(RTTI) and virtual functions implementation. We have also discussed  We hope this blog has helped you enhance your knowledge regarding the C++ concepts. Some official documentation on big data that can help you improve your understanding is this Pointer in C++ and Local class.

If you would like to learn more, check out our articles on Operator Keyword in C#cloud platform comparison, and 10 AWS best books.  Practice makes a man perfect. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.

Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass