1

Should one mark explicitly as virtual all overrides in descendant classes at any level?

class Base {
// ...
protected:
  virtual void to_be_derived() const; // First level to introduce this virtual function
};

class LevelOne : public Base {
// ...
protected:
 // virtual??
 void to_be_derived() const;
};

class LevelTwo : public levelOne {
// ...
protected:
 // virtual??
 void to_be_derived() const;
};

I didn't see the Prefixing virtual keyword to overridden methods which answers my question. In particular, one of the answers there was updated to reflect current usage with respect to c++11, especially the override keyword that I didn't know about!

EDIT: I'd rather accept another answer from the linked question for post-c++11 code.

green diod
  • 1,399
  • 3
  • 14
  • 29
  • 1
    will be virtual by default in dervied class, so add virtual suffix or not, function will be virtual – jonezq Mar 16 '16 at 11:33
  • Note that even the (correct) answer of using `override`/`final` already appears in the above question. – Ami Tavory Mar 16 '16 at 11:36
  • @jonezq I know the function would still be virtual but I see it as a way to *explicitly* convey to the class user that it is virtual. – green diod Mar 16 '16 at 12:23
  • As others have answered - better use new c++11 feature override and final, will give you compilation error, if it doesn't override any base virtual function – jonezq Mar 16 '16 at 12:41
  • @jonezq I wanted to clarify why I wanted to use the `virtual` prefix in reply to your comment about the overridden function being already virtual, which I knew. Now, I didn't know about those new c++11 keywords but `override` is indeed the way to go for post c++11 code – green diod Mar 16 '16 at 12:57

3 Answers3

9

Nowadays, it's better to mark them as override. It tells the reader the function is virtual, and is also a fail-safe mechanism (in case you get the signature wrong).

I'd only use virtual if that was consistent with already existing code.

class LevelOne : public Base {
protected:
   void to_be_derived() const override;
   //                            |
   // clearly virtual, certain it's the same signature as the base class
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
-1

It's better to mark them as virtual and override. Virtual will prevent you from calling wrong function for delivered object. And override will prevent you from mistakes in signature and will make code more readable. As Scott Meyers wrote in effective c++ book, you should't redefine in delevired classes non virtual functions.

Andrey Nekrasov
  • 456
  • 2
  • 12
-2

Base Class Virtual would force the inheriting class ie LevelOne to override it.

Unless you need LevelTwo to override LevelOne's implementation , you do not need to mark it as virtual.

In general, unless the derived class has to override it, no need of using virtual

Max
  • 97
  • 2
  • 13
  • "Unless you need LevelTwo to override LevelOne's implementation , you do not need to mark it as virtual." ??? – jonezq Mar 16 '16 at 11:37
  • 1
    Base class virtual does not force the inheriting class to override it. Also, if the function is virtual in base class, then you don't need to declare it virtual in the middle derived class in order to override in the most derived class. – eerorika Mar 16 '16 at 11:47