References

References and Runtime Polymorphism

Can I use references with polymorphic classes to achieve runtime polymorphism?

3D character art

Absolutely! References are a great way to achieve runtime polymorphism in C++. They allow you to work with derived class objects through base class references, enabling you to call virtual functions and get the correct derived class behavior.

Let's look at an example using our Character class from the lesson:

#include <iostream>
#include <string>

class Character {
 public:
  Character(const std::string& name)
  : mName{name} {}
  virtual void Attack() const {
    std::cout << mName
      << " performs a basic attack!\n";
  }
  virtual ~Character() = default;

 protected:
  std::string mName;
};

class Warrior : public Character {
 public:
  Warrior(const std::string& name)
  : Character{name} {}
  void Attack() const override {
    std::cout << mName
      << " swings a mighty sword!\n";
  }
};

class Mage : public Character {
 public:
  Mage(const std::string& name)
  : Character{name} {}
  void Attack() const override {
    std::cout << mName
      << " casts a powerful spell!\n";
  }
};

void PerformAttack(const Character& character) {
  character.Attack();
}

int main() {
  Character genericChar{"Generic"};
  Warrior warrior{"Conan"};
  Mage mage{"Gandalf"};

  PerformAttack(genericChar);
  PerformAttack(warrior);
  PerformAttack(mage);
}
Generic performs a basic attack!
Conan swings a mighty sword!
Gandalf casts a powerful spell!

In this example:

  1. We define a base Character class with a virtual Attack() method.
  2. We create two derived classes, Warrior and Mage, each overriding the Attack() method.
  3. We define a PerformAttack() function that takes a const Character& parameter.
  4. In main(), we create instances of each class and pass them to PerformAttack().

The key here is the PerformAttack() function. It takes a reference to a Character, but when we call it with a Warrior or Mage, it still calls the correct overridden Attack() method. This is runtime polymorphism in action!

A few important points:

  • The Attack() method must be declared virtual in the base class for this to work.
  • We use const references here as we're not modifying the objects.
  • It's good practice to declare a virtual destructor in the base class when using polymorphism.

References provide a clean syntax for achieving polymorphism, as you don't need to worry about dereferencing pointers. However, remember that unlike pointers, references can't be null and can't be reassigned after initialization.

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

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% 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.

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