Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing pure virtual function from abstract base class: does override specifier have any meaning?

Tags:

c++

Background

I just stumbled over a use case of the override specifier that, as far as I can tell, seems redundant and also without any particular semantics meaning, but maybe I'm missing something, hence this question. Before proceeding, I should point out that I've tried to find an answer to it here on SO, but the nearest I got were the following threads, not really answering my query (maybe someone can point out a Q&A that actually already answers my question).

  • C++ Virtual/Pure Virtual Explained
  • C++ override pure virtual method with pure virtual method

Question

Consider the following abstract class:

struct Abstract {
  virtual ~Abstract() {};
  virtual void foo() = 0;
};

Is there any reason to use the override specifier when implementing foo() in a non-abstract class derived directly from Abstract (as in DerivedB below)? I.e., when the implementation of foo() is already required for the derived class to be non-abstract (and not really overriding anything)?

/* "common" derived class implementation, in my personal experience
   (include virtual keyword for semantics) */
struct DerivedA : public Abstract {
  virtual void foo() { std::cout << "A foo" << std::endl; }
};

/* is there any reason for having the override specifier here? */
struct DerivedB : public Abstract {
  virtual void foo() override { std::cout << "B foo" << std::endl; }
};
like image 429
dfrib Avatar asked Dec 06 '22 16:12

dfrib


2 Answers

I'm not a big fan of override, but, assuming it's something that you find useful in general, then, yes, putting override on a virtual function that overrides a pure virtual functions is useful. Consider this rather contrived example:

struct Base {
    virtual void f() = 0;
};

struct Derived : Base {
    virtual void f();
    virtual void f(int);
};

Now suppose that the maintainer of Base (perhaps even your future self) changes Base to look like this:

struct Base {
    virtual void f(int) = 0;
};

Now the behavior of Derived has quietly changed. With override the compiler would report an error.

like image 90
Pete Becker Avatar answered Feb 16 '23 04:02

Pete Becker


Technically, both versions are syntactically correct and legal. The override specifier is mainly meant to prevent unexpected behavior. The compiler will throw an error as soon as it encounters a member function marked as override which is not actually overriding a virtual function. This may occur if, for some reason, you change the signature of the virtual base class function. Consider this example:

class Abstract {
    virtual void foo() { ...}
}; 

class Derived : public Abstract {
    void foo() override { ... }
};

Now, if the signature of Abstract::foo is changed, let's say to

class Abstract {
    virtual void foo(int bar) { ...}
}; 

the compiler will throw an error at Derived::foo as it no longer overrides a function of Abstract which it wouldn't without the override qualifier. This helps you to better maintain your code. However, in your specific case (i.e. with pure virtual declarations), an error would be thrown as well. So using override is mainly considered "good practice", I guess. More on that topic: http://en.cppreference.com/w/cpp/language/override

like image 36
Felix Lauer Avatar answered Feb 16 '23 03:02

Felix Lauer