Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 897
Eric Avatar asked Jun 26 '13 10:06

Eric


People also ask

Can virtual methods be overridden?

A virtual method defined in the base class can be overridden in a derived class by defining a method with the same signature and marking it with the override keyword.

Can we override non-virtual method in Java?

Base: Non-virtual display. because the Display method of the Base class is not a virtual method so the Derived class should not be able to override it.


1 Answers

From section 10.3 Virtual functions of the c++11 standard (draft n3337) point 2:

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name, parameter-type-list (8.3.5), cv-qualification, and refqualifier (or absence of same) as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.

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

like image 77
hmjd Avatar answered Sep 18 '22 04:09

hmjd