Creating SDL2 Buttons

The override Keyword in C++ Explained

What does the override keyword do in C++ when used with class methods?

Abstract art representing computer programming

The override keyword in C++ is a special specifier that you can add at the end of a member function declaration or definition in a derived class. Its purpose is to clearly state your intent that this function is meant to override a virtual function from one of its base classes.

// Base class
class Base {
public:
  virtual void DoSomething() { /* ... */ }
  virtual void AnotherFunc(int x) { /* ... */ }
  void NonVirtualFunc() { /* ... */ }
};

// Derived class
class Derived : public Base {
public:
  // Correctly overrides Base::DoSomething
  void DoSomething() override { /* ... */ } 

  // Also correct
  virtual void AnotherFunc(int x) override { /* ... */ } 

  // ERROR: NonVirtualFunc is not virtual in Base
  // void NonVirtualFunc() override {} // Won't compile

  // ERROR: Typo in name, no Base function matches
  // void DoSomethang() override {} // Won't compile

  // ERROR: Different signature, no Base function matches
  // void AnotherFunc(float x) override {} // Won't compile
};

Benefits of Using override

Using override provides two significant benefits:

Compile-Time Safety

This is the primary advantage. The compiler verifies that your function actually overrides a base class virtual function. If it doesn't match any base class virtual function signature precisely (same name, same parameter types, same const/volatile qualification, same ref-qualifiers), the compiler will generate an error.

Without override****: If you make a typo in the function name or its parameters, you might accidentally create a new, unrelated function in the derived class instead of overriding the base class one. Your code will compile, but polymorphism won't work as expected – calls through a base class pointer might call the base version instead of your intended derived version.

class DerivedOops : public Base {
public:
  // TYPO! No override keyword used.
  // This compiles, but creates a NEW function,
  // it DOES NOT override Base::DoSomething()!
  virtual void DoSomethang() { 
    // ... oops ...
  }
};

Base* ptr = new DerivedOops();
ptr->DoSomething(); // Calls Base::DoSomething(),
                    // NOT DerivedOops::DoSomethang()!

With override****: If you make the same typo but include override, the compiler immediately tells you something is wrong.

class DerivedSafe : public Base {
public:
  // TYPO! But override keyword IS used.
  virtual void DoSomethang() override {
    // ...
  }
  // COMPILER ERROR: 'DoSomethang' marked override
  // but does not override any base class method.
};

Improved Readability and Maintainability

The override keyword clearly documents the programmer's intention. Anyone reading the code immediately knows that this function is part of a polymorphism hierarchy and is specifically designed to replace a base class implementation. This makes the code easier to understand and safer to modify later.

When to Use It

You should use override whenever you intend for a function in a derived class to override a virtual function from a base class. It's considered modern C++ best practice. While not strictly required for overriding to occur (if the signature matches perfectly), the safety net and clarity it provides are invaluable.

Note that override can only be used on virtual functions in derived classes. Trying to use it on non-virtual functions or on functions in a class with no base classes will result in a compiler error.

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

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

This course includes:

  • 110 Lessons
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQs
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 © 2025 - All Rights Reserved