0

Linked question is not the same - and does not even mention override

Edit: The new list of duplicates contains one legitimate duplicate, which I did not find from search.

I was not aware prior to asking this that the choice of whether or not to use virtual in derived class members was going to be a contentious issue for some.


I have just encountered some source code which looks like this:

class A
{
    virtual void method();
};

class B : public A
{
    void method() override;
}

I am unsure of how to interpret this, even after reading this.

Does override imply virtual here? void B::method() is not marked as a virtual function, but it is marked as override. So why does this work, and not result in a compilation error?

Is there any difference between the following? (In class B)

  • void method() override;
  • virtual void method() override;
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • 1
    Because `method` is virtual in the base class, `method` is also virtual in the derived class. I recall seeing at least one style guide that argued it is good style (but not necessary) to mark it virtual explicitly in the derived class as a reminder, and to be resistant to any refactoring of the base class. Worth saying that it's not the keyword `override` that makes it virtual. – Wyck Aug 15 '22 at 14:47
  • There is no way to make a function non-virtual. You can override it, or seal it (final) but it will stay virtual in all derived classes. And it is not even necessary, but good manners. A function in derived type which matches name and parameters etc will override a virtual function in base class. – Tanveer Badar Aug 15 '22 at 14:48
  • 1
    Here is the guideline for the use of virtual/override/final keywords : [C.128: Virtual functions should specify exactly one of virtual, override, or final](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rh-override). So doing both `virtual` AND `override` together is not good style. – Pepijn Kramer Aug 15 '22 at 14:49
  • @Wyck Thinking about it, I have never *not* done this - because it might be confusing – FreelanceConsultant Aug 15 '22 at 14:53
  • @FreelanceConsultant A member function in the derived class with the same signature as a virtual member function of base class is `virtual`. See dupe: [Why is a virtual function in a derived class also virtual?](https://stackoverflow.com/questions/65872341/why-is-a-virtual-function-in-a-derived-class-also-virtual). This is explained in any beginner level [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Jason Aug 15 '22 at 14:57
  • @JasonLiam It's not the same question. – FreelanceConsultant Aug 15 '22 at 14:58
  • @FreelanceConsultant It is the same question. You seem to not know that `void method() override` and `virtual void method() override` are the same(equivalent) thing inside the derived class. – Jason Aug 15 '22 at 14:59
  • @JasonLiam I did not know that `virtual` is optional in a derived class, but this is not the question which was asked here. The answers may be linked but the questions are totally unrealated. – FreelanceConsultant Aug 15 '22 at 15:00
  • @FreelanceConsultant The fundamental reason is the same. If you understand the principle, you will understand the answer for this question also. I've added another dupe: [Should I use virtual, override, or both keywords?](https://stackoverflow.com/a/39932616/12002570). Note that in the second dupe, it is clearly mentioned that: *"When you override a function you don't technically need to write either `virtual` or `override`."* – Jason Aug 15 '22 at 15:02
  • @JasonLiam your second linked question is a duplicate. The first is not. Do you see the word `override` used anywhere in the first question? No? Then it's not a duplicate. – FreelanceConsultant Aug 15 '22 at 15:06
  • Yes, I did already search both this site and google for an answer to this question. I also provided a link to a reference which I thought should provide the answer, but didn't. – FreelanceConsultant Aug 15 '22 at 15:07
  • I have also read perhaps a dozen C++ textbooks recently and nowhere do I recall this somewhat unusual feature being mentioned. Perhaps because most authors advise not to write code like this anyway, which was also my opinion on the matter... – FreelanceConsultant Aug 15 '22 at 15:08
  • @FreelanceConsultant It's fine,i've reshuffled the dupe list so that the second dupe is now the first. Also, note in any introductory c++ book talking about inheritance you'll definitely find something like: *"A member function in the derived class with the same signature as a virtual member function of base class is virtual. "* I am sure about this because this is a very important part of inheritance in c++ and so books usually explicitly mention this part. Also, note that i am talking about introductory book that explain things and not advanced books that assume that one already knows this. – Jason Aug 15 '22 at 15:14
  • @FreelanceConsultant Continued... You most probably missed or overlooked that part in the book. – Jason Aug 15 '22 at 15:16
  • @JasonLiam which book, there's a whole shelf of them – FreelanceConsultant Aug 15 '22 at 15:34
  • @JasonLiam Literally name one book which has this. And not something like "C++ for beginners" I don't own any beginner level textbooks – FreelanceConsultant Aug 15 '22 at 15:34
  • @FreelanceConsultant For one, the goto book that i recommend to beginners which is named *"C++ Primer by Lippman"* for one has this description. It is one of the best beginner level books that is used by people when starting out with c++(including me). – Jason Aug 15 '22 at 15:41
  • @JasonLiam I don't own this book – FreelanceConsultant Aug 15 '22 at 15:59
  • @JesperJuhl The duplicates already answered the question. There were three duplicates in the dupe list. There was no need to reopen this. – Jason Aug 15 '22 at 16:49
  • @JasonLiam While I agree one of those is a duplicate, I would ask the following question: *Does it really matter?* It appears that you were unhappy about this question being posted origionally, I do not know why. Now it has been re-opened and you are further upset by that fact. Yes, the correct thing to happen now is to close the question as a duplicate *with the actual duplicate question linked, not all of the ones which are not duplicates*. I will happily close it myself if you post the link (to the correct duplicate) again. – FreelanceConsultant Aug 15 '22 at 19:39
  • I have now closed the question with a link to *a* duplicate, but it is not the same one as earlier. So I guess there is more than one duplicate of this question currently in existance on this site... – FreelanceConsultant Aug 15 '22 at 19:42

4 Answers4

5

The answer you're looking for is in https://en.cppreference.com/w/cpp/language/virtual

If some member function vf is declared as virtual in a class Base, and some class Derived, which is derived, directly or indirectly, from Base, has a declaration for member function with the same name parameter type list (but not the return type) cv-qualifiers ref-qualifiers Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).

TheUndeadFish
  • 8,058
  • 1
  • 23
  • 17
  • Learn something new everyday. I didn't know you could omit virtual in a derived class... Although doing so is probably not advisable since it may be a source of confusion – FreelanceConsultant Aug 15 '22 at 14:51
  • 5
    @FreelanceConsultant: This is kind of the point of `override`. It doesn't mearly "imply" `virtual`; it *requires* `virtual` (implicit or explicit). And if the base class has no matching function, you get a compile error. So derived classes should always use `override` when overriding base class functions. – Nicol Bolas Aug 15 '22 at 14:53
3

Does override imply virtual here?

Yes. Only virtual methods can be overridden. override on a non-virtual method is an error.

void B::method() is not marked as a virtual function,[..]

It is sufficient to declare a method to be virtual in the base class. In all classes deriving from A the method is virtual. Adding the virtual specifier to B::method would be redundant.

Is there any difference between the following? [...]

It is good for readability to mention also in the derived class that a method is virtual. Though, as only virtual methods can be overridden, it is sufficient to specify override to make it clear that it is a virtual method.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

Yes, override implies virtual in the sense that if the function was not already declared virtual in a base class then override would be an error (and diagnosed as such by the compiler) and adding both virtual and override to the same function is redundant (just as adding the virtual keyword in derived classes is redundant - functions that are virtual in base classes are always virtual in derived classes, even if not explicitly marked as such). However, that also means that override does not make a function virtual if it wasn't already.

The code you show is perfectly sensible. It clearly communicated (via the override keyword) that the function is virtual and also overrides a virtual function in a base class (all with just one keyword).

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
0

The override keyword simply means that this function is overriding a virtual function from the base class.

Best practice is to use virtual in base classes and override in derived (mainly for readability and convenience).

Another important keyword is the "final" keyword (comes right after override at the function header)

You can use it to specify this is the last override on the virtual function, thus it disables any further overriding (this, of course, is only useful when dealing with a derived class of an derived class).

RoyA
  • 46
  • 5