Run Time Type Information (RTTI) and typeid()

RTTI with Abstract Base Classes

Is it possible to use RTTI with abstract base classes? If so, how?

Illustration representing computer hardware

Yes, it is absolutely possible to use RTTI (Run-Time Type Information) with abstract base classes.

In fact, RTTI is particularly useful when working with abstract base classes and their derived concrete classes. Let's explore how this works and some practical applications.

RTTI and Abstract Classes

An abstract class in C++ is a class that has at least one pure virtual function. While you can't create an instance of an abstract class directly, you can use pointers or references to the abstract class to refer to objects of its derived classes.

This is where RTTI becomes valuable. Here's a simple example:

#include <iostream>
#include <typeinfo>

// Abstract base class
class Shape {
 public:
  virtual ~Shape() = default;

  // Pure virtual function
  virtual double Area() const = 0;
};

class Circle : public Shape {
 public:
  Circle(double radius) : radius_(radius) {}
  double Area() const override {
    return 3.14159 * radius_ * radius_;
  }
 private:
  double radius_;
};

class Rectangle : public Shape {
 public:
  Rectangle(double width, double height)
    : width_(width), height_(height) {}
  double Area() const override {
    return width_ * height_;
  }
 private:
  double width_, height_;
};

int main() {
  Shape* shape1 = new Circle(5);
  Shape* shape2 = new Rectangle(4, 6);

  std::cout << "shape1 is a "
    << typeid(*shape1).name() << "\n";
  std::cout << "shape2 is a "
    << typeid(*shape2).name() << "\n";

  delete shape1;
  delete shape2;
}
shape1 is a class Circle
shape2 is a class Rectangle

In this example, Shape is an abstract base class, and we're using typeid() to determine the actual types of objects pointed to by Shape* pointers.

Using dynamic_cast with Abstract Classes

dynamic_cast is particularly useful with abstract base classes:

#include <iostream>
#include <typeinfo>

class Shape {/*...*/}
class Circle : public Shape {/*...*/}
class Rectangle : public Shape {/*...*/} void PrintArea(Shape* shape) { if (auto* circle = dynamic_cast<Circle*>(shape)) { std::cout << "Circle area: " << circle->Area() << "\n"; } else if (auto* rectangle = dynamic_cast<Rectangle*>(shape)) { std::cout << "Rectangle area: " << rectangle->Area() << "\n"; } else { std::cout << "Unknown shape\n"; } } int main() { Shape* shape1 = new Circle(5); Shape* shape2 = new Rectangle(4, 6); PrintArea(shape1); PrintArea(shape2); delete shape1; delete shape2; }
Circle area: 78.5397
Rectangle area: 24

RTTI in Factory Methods

RTTI can be useful in factory methods that return pointers to abstract base classes:

#include <iostream>
#include <typeinfo>

class Shape {/*...*/}
class Circle : public Shape {/*...*/}
class Rectangle : public Shape {/*...*/} enum class ShapeType { Circle, Rectangle }; std::unique_ptr<Shape> CreateShape(ShapeType type) { switch (type) { case ShapeType::Circle: return std::make_unique<Circle>(1.0); case ShapeType::Rectangle: return std::make_unique<Rectangle>(1.0, 1.0); default: throw std::invalid_argument("Unknown shape type"); } } int main() { auto shape = CreateShape(ShapeType::Circle); std::cout << "Created shape is a " << typeid(*shape).name(); }
Created shape is a class Circle

Considerations

While RTTI is powerful, it's important to use it judiciously. Overreliance on RTTI can sometimes indicate a design that could be improved through better use of polymorphism. Additionally, RTTI does come with a small runtime cost and can increase the size of your executable.

In conclusion, RTTI works well with abstract base classes and can be a valuable tool in your C++ toolbox. It allows you to determine the concrete type of an object at runtime, which can be especially useful when working with collections of pointers to abstract base classes.

This Question is from the Lesson:

Run Time Type Information (RTTI) and typeid()

Learn to identify and react to object types at runtime using RTTI, dynamic casting and the typeid() operator

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Run Time Type Information (RTTI) and typeid()

Learn to identify and react to object types at runtime using RTTI, dynamic casting and the typeid() operator

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved