In C++, to what kind of class can you apply Run Time Type Information (RTTI) features such as dynamic_cast and typeid, in order to identify the actual derived type at run time?

Difficulty: Medium

Correct Answer: Polymorphic

Explanation:


Introduction / Context:
Run Time Type Information (RTTI) in C++ allows programs to query the actual type of objects at run time, mainly through features like dynamic_cast and typeid. However, RTTI works only under certain conditions. This question tests whether you know that RTTI requires polymorphic classes, that is, classes with at least one virtual function.


Given Data / Assumptions:

  • We are using C++ with classes that may or may not contain virtual functions.
  • We want to safely downcast base class pointers or references to derived classes.
  • We intend to use dynamic_cast or typeid to inspect the actual run time type.


Concept / Approach:
RTTI relies on run time type information stored in objects of polymorphic classes. A class becomes polymorphic when it has at least one virtual function, often a virtual destructor or virtual method. For dynamic_cast to work on pointers or references from a base type to a derived type, the base class must be polymorphic. Otherwise, the behaviour of dynamic_cast on references is undefined and on pointers simply returns a null pointer when casting down. The key idea is that RTTI is fundamentally tied to polymorphism in C++.


Step-by-Step Solution:
Step 1: Define a base class with a virtual function: struct Base { virtual ~Base() {} }; Step 2: Derive another class: struct Derived : Base { }; Step 3: Use a Base* pointer pointing to a Derived object and apply dynamic_cast<Derived*>(pointer); which uses RTTI to check the actual type. Step 4: This works correctly because Base is polymorphic due to the virtual destructor. Step 5: If you remove the virtual function, Base is no longer polymorphic, and RTTI for downcasting via dynamic_cast is not properly supported.


Verification / Alternative check:
Experimenting with code shows that if you attempt to use dynamic_cast between non polymorphic types, the compiler may reject the code or the cast will not perform run time checks as expected. In contrast, when the base class is polymorphic, dynamic_cast can safely detect whether the object is actually of the target derived type. This confirms that RTTI applies to polymorphic classes.


Why Other Options Are Wrong:
Option A, Encapsulation, is a general object oriented principle and not a specific class category that enables RTTI. Option C, Derived, is too vague; derived classes can be polymorphic or non polymorphic. Option D is wrong because RTTI clearly applies to classes that are polymorphic by virtue of having at least one virtual function.


Common Pitfalls:
A typical pitfall is trying to use dynamic_cast with a base class that has no virtual functions, expecting it to work like in fully polymorphic hierarchies. Another mistake is overusing RTTI when virtual functions and proper design can remove the need to query types explicitly. Understanding that RTTI is a tool specifically for polymorphic classes encourages cleaner, more idiomatic C++ designs.


Final Answer:
RTTI in C++ applies to polymorphic classes, that is, classes with at least one virtual function, so the correct option is Polymorphic.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion