Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ override specifier without virtual? Does override imply virtual?

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading