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 problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow. Happy Coding!