C++ final specifier

Cast all your worries on him,
because he cares for you.
1 Peter 5: 7 (Bible)

In Java, we can use final for a function to make sure that it cannot be overridden. We can also use final in Java to make sure that a class cannot be inherited. Similarly, the latest C++ standard C++ 11 added final.

Use of final specifier in C++ 11:
Sometimes you don’t want to allow derived class to override the base class’ virtual function. C++ 11 allows built-in facility to prevent overriding of virtual function using final specifier.
Consider the following example which shows use of final specifier. This program fails in compilation.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
class Base
{
public:
    virtual void myfun() final
    {
        cout << "myfun() in Base";
    }
};
class Derived : public Base
{
    void myfun()
    {
        cout << "myfun() in Derived\n";
    }
};
  
int main()
{
    Derived d;
    Base &b = d;
    b.myfun();
    return 0;
}

chevron_right


Output:

prog.cpp:14:10: error: virtual function ‘virtual void Derived::myfun()’
     void myfun()
          ^
prog.cpp:7:18: error: overriding final function ‘virtual void Base::myfun()’
     virtual void myfun() final 

2nd use of final specifier:
final specifier in C++ 11 can also be used to prevent inheritance of class / struct. If a class or struct is marked as final then it becomes non inheritable and it cannot be used as base class/struct.
The following program shows use of final specifier to make class non inheritable:

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
class Base final
{
};
  
class Derived : public Base
{
};
  
int main()
{
    Derived d;
    return 0;
}

chevron_right


Output:



error: cannot derive from ‘final’ base ‘Base’ in derived type ‘Derived’
 class Derived : public Base

final in C++ 11 vs in Java
Note that use of final specifier in C++ 11 is same as in Java but Java uses final before the class name while final specifier is used after the class name in C++ 11. Same way Java uses final keyword in the beginning of method definition (Before the return type of method) but C++ 11 uses final specifier after the function name.

filter_none

edit
close

play_arrow

link
brightness_4
code

class Test
{
    final void fun()// use of final in Java
    { }
}
class Test
{
public:
    virtual void fun() final //use of final in C++ 11
    {}
};

chevron_right


Unlike Java, final is not a keyword in C++ 11. final has meaning only when used in above contexts, otherwise it’s just an identifier.
One possible reason to not make final a keyword is to ensure backward compatibility. There may exist production codes which use final for other purposes. For example the following program compiles and runs without error.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
int main()
{
    int final = 20;
    cout << final;
    return 0;
}

chevron_right


Output:

20

In java, final can also be used with variables to make sure that a value can only be assigned once. this use of final is not there in C++ 11.

This article is contributed Meet Pravasi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.