JoeCool
JoeCool

Reputation: 159

Combination of override and virtual in c++11

I'd like to use the C++11 override specifier and noticed that it can be used together with virtual. Does this make a difference? Consider the following classes:

class Base
{
    virtual void fct() = 0;
};

class Derived1 : public Base
{
    void fct() override {}
};

class Derived2 : public Base
{
    virtual void fct() override {}
};

Is there a technical difference between Derived1 and Derived2?

Upvotes: 2

Views: 232

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

override is always used together with virtual. It is used to mandate that your virtual function overrides a virtual function in the base class.

An older rule means that, if it does override a virtual function in the base class, it's virtual automatically so you can skip writing the virtual out in your code. But "conceptually" the virtual is there already anyway.

So, writing virtual there will make no difference at all. Since you wrote override, the function must override a base class function (and, by extension, it must be virtual), otherwise your code won't compile.

Upvotes: 4

Related Questions