Wikipedia has the following example on the C++11 final modifier:
struct Base2 {
virtual void f() final;
};
struct Derived2 : Base2 {
void f(); // ill-formed because the virtual function Base2::f has been marked final
};
I don't understand the point of introducing a virtual function and immediately marking it as final. Is this simply a bad example, or is there more to it?
While refactoring legacy code (e.g. removing a method that is virtual from a mother class), this is useful to ensure none of the child classes are using this virtual function.
Typically
final
will not be used on the base class' definition of a virtual function.final
will be used by a derived class that overrides the function in order to prevent further derived types from further overriding the function. Since the overriding function must be virtual normally it would mean that anyone could override that function in a further derived type.final
allows one to specify a function which overrides another but which cannot be overridden itself.For example if you're designing a class hierarchy and need to override a function, but you do not want to allow users of the class hierarchy to do the same, then your might mark the functions as final in your derived classes.
It doesn't seem useful at all to me. I think this was just an example to demonstrate the syntax.
One possible use is if you don't want f to really be overrideable, but you still want to generate a vtable, but that is still a horrible way to do things.
For a function to be labelled
final
it must bevirtual
, i.e., in C++11 §10.3 para. 2:and para 4:
i.e.,
final
is required to be used with virtual functions (or with classes to block inheritance) only. Thus, the example requiresvirtual
to be used for it to be valid C++ code.EDIT: To be totally clear: The "point" asked about concerns why virtual is even used. The bottom-line reason why it is used is (i) because the code would not otherwise compile, and, (ii) why make the example more complicated using more classes when one suffices? Thus exactly one class with a virtual final function is used as an example.
The purpose of that example is to illustrate how
final
works, and it does just that.A practical purpose might be to see how a vtable influences a class' size.
Base2
can simply be used to test platform specifics, and there's no point in overridingf()
since it's there just for testing purposes, so it's markedfinal
. Of course, if you're doing this, there's something wrong in the design. I personally wouldn't create a class with avirtual
function just to check the size of thevfptr
.virtual
+final
are used in one function declaration for making the example short.Regarding the syntax of
virtual
andfinal
, the Wikipedia example would be more expressive by introducingstruct Base2 : Base1
with Base1 containingvirtual void f();
and Base2 containingvoid f() final;
(see below).Standard
Referring to N3690:
virtual
asfunction-specifier
can be part ofdecl-specifier-seq
final
can be part ofvirt-specifier-seq
There is no rule having to use the keyword
virtual
and the Identifiers with special meaningfinal
together. Sec 8.4, function definitions (heed opt = optional):Practice
With C++11, you can omit the
virtual
keyword when usingfinal
. This compiles on gcc >4.7.1, on clang >3.0 with C++11, on msvc, ... (see compiler explorer).PS: The example on cppreference also does not use virtual together with final in the same declaration.
PPS: The same applies for
override
.