Run Time Type Information (RTTI) and typeid()

RTTI and Smart Pointers

How can I use RTTI in conjunction with smart pointers to manage polymorphic objects safely?

Illustration representing computer hardware

Using RTTI with smart pointers can provide a powerful combination for managing polymorphic objects safely. Here's how you can effectively use them together:

Type Checking with dynamic_pointer_cast()

Instead of using dynamic_cast() with raw pointers, use std::dynamic_pointer_cast() with smart pointers. This function performs a safe downcast and returns a null pointer if the cast fails.

#include <iostream>
#include <memory>

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

class Dragon : public Monster {
 public:
  void breatheFire() {
    std::cout << "Dragon breathes fire!\n";
  }
};

void handleMonster(
  std::shared_ptr<Monster> monster
) {
  auto dragon =
    std::dynamic_pointer_cast<Dragon>(monster);  
  if (dragon) {
    dragon->breatheFire();
  } else {
    std::cout << "This is not a dragon.\n";
  }
}

int main() {
  auto dragon = std::make_shared<Dragon>();
  auto monster = std::make_shared<Monster>();

  handleMonster(dragon);
  handleMonster(monster);
}
Dragon breathes fire!
This is not a dragon.

Using typeid() with Smart Pointers

You can use typeid() with smart pointers by dereferencing them. This is safe because smart pointers ensure the object exists.

#include <iostream>
#include <memory>
#include <typeinfo>

class Monster {/*...*/};
class Dragon : public Monster {/*...*/}; void identifyMonster( const std::shared_ptr<Monster>& monster ) { if (typeid(*monster) == typeid(Dragon)) { std::cout << "This is a Dragon!\n"; } else { std::cout << "This is a different kind of Monster.\n"; } } int main() { auto dragon = std::make_shared<Dragon>(); auto monster = std::make_shared<Monster>(); identifyMonster(dragon); identifyMonster(monster); }
This is a Dragon!
This is a different kind of Monster.

Custom Type Identification with Smart Pointers

If you prefer a custom type identification system, you can still use it effectively with smart pointers.

#include <iostream>
#include <memory>

enum class MonsterType { Base, Dragon };

class Monster {
 public:
  virtual ~Monster() {}
  virtual MonsterType getType() const {
    return MonsterType::Base;
  }
};

class Dragon : public Monster {
 public:
  MonsterType getType() const override {
    return MonsterType::Dragon;
  }
  void breatheFire() {
    std::cout << "Dragon breathes fire!\n";
  }
};

void handleMonster(
  const std::shared_ptr<Monster>& monster
) {
  if (monster->getType() == MonsterType::Dragon) {  
    auto dragon =
      std::static_pointer_cast<Dragon>(monster);
    dragon->breatheFire();
  } else {
    std::cout << "This is not a dragon.\n";
  }
}

int main() {
  auto dragon = std::make_shared<Dragon>();
  auto monster = std::make_shared<Monster>();

  handleMonster(dragon);
  handleMonster(monster);
}
Dragon breathes fire!
This is not a dragon.

By combining RTTI with smart pointers, you get the benefits of runtime type checking and automatic memory management, leading to safer and more robust code when dealing with polymorphic objects.

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