• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

C++ What's the difference between final & override?

Status
Not open for further replies.

Digital

Learning C++
Joined
Jul 15, 2018
Messages
57
Solutions
1
Reaction score
12
Can anyone explain in laymens terms what is the difference between final & override in a class? Specifically the classes in the sources.
 
override specifier (since C++11) - cppreference.com
final specifier (since C++11) - cppreference.com

Using inheritance, the keyword override it overrides/overload/rewrite a virtual inherited function, only if that function doesn't has the final keyword, since it makes non-overridable/constant

PHP:
#include <iostream>
class A
{
    public:
        void sayHi(); //cannot be overridable because is not virtual

        virtual void sayBye(); //can be overridable because is virtual

        virtual void say() final; //is a virtual function but can not be overridable because is final

       
};

class B : A
{
    public:
        void sayHi() override;
        void sayBye() override;
        void say() override;
};

the code will throw those errors
Code:
main.cpp:26:14: error: 'void B::sayHi()' marked override, but does not override
main.cpp:35:14: error: virtual function 'virtual void B::say()'
main.cpp:16:22: error: overriding final function 'virtual void A::say()'
 
any further virtual methods that are inheritable cannot be overridden after you use the final specifier
which is why in his example he has this in class A
C++:
virtual void say() final;
in class B you can't override say() because in the parent class it has already been finalized
 
Status
Not open for further replies.
Back
Top