Is there any point in using `override` when overriding a pure virtual function?

C++C++11OverridingPure Virtual

C++ Problem Overview


For example:

class Base {
  virtual void my_function() = 0;
};

class Derived : Base {
  void my_function() override;
};

From what I read, the override keyword is used to make sure that we have the correct signature in the function that we are overriding, and it seems to be its only use.

However, in the case of a pure virtual function, the compiler would throw an error if we used an incorrect signature in the Derived class (or Base class, depending on how one see things). So, is there any point in adding override at the end of Derived::my_function() declaration?

C++ Solutions


Solution 1 - C++

> However, in the case of a pure virtual function, the compiler would throw an error if we used an incorrect signature in the Derived class

No, this compiles:

class Base {
  virtual void my_function() = 0;
};

class Derived : Base {
  void my_function(int);
//                 ^^^ mistake!
};

While this does not:

class Base {
  virtual void my_function() = 0;
};

class Derived : Base {
  void my_function(int) override;
};

> error: void Derived::my_function(int) marked override, but does not override >


The error you're talking about only occurs when instantiating Derived - override allows you to catch the mistake earlier and makes the definition of Derived clearer/more readable.

Solution 2 - C++

Yes, it is a good idea to use override keyword consistently as a defensive practice.

Consider a redesign when the author of the Base decides that my_function should no longer be a pure virtual, and also that it should take a new parameter. With override in place the compiler will catch this problem; without an override your Derived class would continue to compile.

Solution 3 - C++

Yes !!

It improves code clarity: override keyword prevents ambiguity and convey it's meaning of overriding its base class method.

Prevents possible unintended usage: In future, if base class change method signature(here virtual) , it force derive class to change accordingly. (with compiler error). Otherwise(with-out override keyword ) it could be considered method overload, which is not intended.

Solution 4 - C++

Usually not bothering with override just moves an error around. I find the location where you get the error better -- at the point where you define the method that fails to override, instead of when you instantiate the class.

But, there is a way for this to protect against a runtime bug.

struct Base {
  virtual void foo(int x = 0) = 0;
  
  void foo(double d) {
      foo( (int)d );
  }
};
inline void Base::foo(int x) { std::cout << "Default foo(" << x << ")\n"; }

struct Derived:Base {
  using Base::foo;
  virtual void foo() { // oops, no int!
    std::cout << "Derived::foo()\n";
    Base::foo();
  }
};
struct Derived2:Derived {
  virtual void foo(int x=0) override {
    std::cout << "Derived2::foo()\n";
    Derived::foo(x);
  }
};

here we intend for each foo to call its parent foo. But because Derived::foo doesn't override the same signature as Base::foo, it isn't invoked.

Add override after foo() in Derived and we get a compile time error.

And yes, I implemented the pure virtual function Base::foo.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionR2B2View Question on Stackoverflow
Solution 1 - C++Vittorio RomeoView Answer on Stackoverflow
Solution 2 - C++Sergey KalinichenkoView Answer on Stackoverflow
Solution 3 - C++JharPaatView Answer on Stackoverflow
Solution 4 - C++Yakk - Adam NevraumontView Answer on Stackoverflow