@overrides for C++?

c++

Solution

It is possible in C++11, with the `override` identifier:

struct Base {    
  virtual void foo() const { std::cout << "Base::foo!\n"; }
};

struct Derived : virtual public Base {
  virtual void foo() const override {std::cout << "Derived::foo!\n";}
};

This allows you to find out at compile time whether you are failing to override a method. Here, we neglect to make the method `const`:

struct BadDerived : virtual public Base {
  virtual void foo() override {std::cout << "BadDerived::foo!\n";} // FAIL! Compiler finds our mistake.

};

Problem

Is there a way in C++ to ensure that a virtual method in a subclass is in fact overriding a super class virtual method? Sometimes when I refactor, I forget a method and then wonder why it is not being called but I forgot to change the method signature so it is no longer overriding anything. Thanks

Original source