Can a virtual function be overridden by a non-virtual function?

In this code:

class Base { public: virtual void method() = 0; }; class Derived1 : public Base{ public: virtual void method() override { } }; class Derived2 : public Base{ public: void method() override { } }; 

Is there any difference between Derived1 and Derived2 ?

+6
source share
2 answers

From section 10.3 Virtual Functions of C ++ 11 Standard (draft n3337), clause 2:

If the virtual vf member function is declared in the Base class and in the Derived class obtained directly or indirectly from Base, the vf member function with the same name, a list of parameter parameters (8.3.5), cv-qualification and refqualifier (or lack of the same) as Base :: vf, then Derived :: vf is also virtual (regardless of what is declared) , and it overrides Base :: vf.

So, Derived2::method also virtual , although it is not explicitly declared as such.

+16
source

They are identical.

virtual is optional when actually overriding a function. It is mandatory only when marking functions in the base class.

+4
source

Source: https://habr.com/ru/post/948129/


All Articles