简体   繁体   中英

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM