You are on page 1of 3

RTTI (Runtime Type Information) is a mechanism in C++ that provides information

about an object's data type at runtime. This information is typically stored as part of
an object's class hierarchy, and it allows programs to determine the type of an object
even if the type of the object is not known at compile-time. RTTI enables a number
of important programming techniques, including dynamic dispatch, dynamic casting,
and reflection.

RTTI was added to the C++ language because many vendors of class libraries were
implementing this functionality themselves. This caused incompatibilities between
libraries. Thus, it became obvious that support for run-time type information was
needed at the language level.

For the sake of clarity, this discussion of RTTI is almost completely restricted to
pointers. However, the concepts discussed also apply to references.

There are three main C++ language elements to run-time type information:

    The dynamic_cast operator - Used for conversion of polymorphic types.


    The typeid operator -  Used for identifying the exact type of an object.
    The type_info class -  Used to hold the type information returned by the
typeid operator.

What is RTTI used for in C++?

 To retrieve information about an object's data type at runtime.


 To cast an object from one type to another type at runtime.

 To check if an object is of a specific type at runtime.

typeid is an operator in C++ that provides information about an object's data type
at runtime. It is part of the RTTI (Runtime Type Information) mechanism in C++.

The typeid operator takes an expression as an argument and returns


a type_info object that describes the type of the expression. The type_info object
can then be used to compare two types, or to retrieve the name of a type as a string.

The typeid operator is a powerful tool for writing generic code, as it allows you to


determine the type of an object even if the type of the object is not known at
compile-time. However, it does come with a performance overhead, so it should be
used with care.
Here is an example to demonstrate the use of the typeid operator:
#include <iostream>
#include <typeinfo>

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

class Circle : public Shape {


};

class Square : public Shape {


};

int main() {
  Shape *shape = new Circle;
  std::cout << "The type of the shape is: " << typeid(*shape).name() <<
std::endl;
  return 0;
}

In this example, we have a base class Shape, and two derived


classes, Circle and Square. In main(), we create a pointer to a Shape object and
assign it to a new Circle object. We then use the typeid operator to retrieve the type
information of the shape object and print the name of the type using
the name() method of the type_info object.

Dynamic casting is a mechanism in C++ that allows you to cast a pointer or reference
from one type to another type, where the target type is not known until runtime. This
is often used in object-oriented programming when you have a base class pointer
that needs to be cast to a derived class pointer. Dynamic casting is implemented
using RTTI (Runtime Type Information).

Dynamic casting is performed using the dynamic_cast operator. The dynamic_cast


operator can be used to cast a pointer or reference from one type to another type,
and it will return nullptr or an exception if the cast is not valid.

Dynamic casting is a safer alternative to static_cast when casting between types that


are not known at compile-time, as it performs a runtime check to ensure that the cast
is valid. However, it does come with a performance overhead, so it should be used
with care.

Here is an example to demonstrate dynamic casting in C++:


#include <iostream>

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

class Circle : public Shape {


};

class Square : public Shape {


};

int main() {
  Shape *shape = new Circle;
  Circle *circle = dynamic_cast<Circle*>(shape);
  if (circle) {
    std::cout << "The shape is a Circle." << std::endl;
  } else {
    std::cout << "The shape is not a Circle." << std::endl;
  }
  return 0;
}

In this example, we have a base class Shape and two derived classes, Circle and
Square. In main(), we create a pointer to a Shape object and assign it to a new Circle
object. We then use the dynamic_cast operator to cast the shape pointer to a Circle*
pointer. If the cast is successful, the circle pointer will point to the Circle object, and
the output will be The shape is a Circle.. If the cast is not successful, the circle pointer
will be nullptr, and the output will be The shape is not a Circle.

What will be the output for the above example.?

 Runtime error
 The shape is not a Circle.
 The shape is a Circle.
 None of these.
Close

You might also like