What is the purpose of the final
keyword in C++11 for functions? I understand it prevents function overriding by derived classes, but if this is the case, then isn't it enough to declare as non-virtual your final
functions? Is there another thing I'm missing here?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
"final" also allows a compiler optimization to bypass the indirect call:
with "final", the compiler can call
CDerived::DoSomething()
directly from withinBlah()
, or even inline. Without it, it has to generate an indirect call inside ofBlah()
becauseBlah()
could be called inside a derived class which has overriddenDoSomething()
.The final keyword allows you to declare a virtual method, override it N times, and then mandate that 'this can no longer be overridden'. It would be useful in restricting use of your derived class, so that you can say "I know my super class lets you override this, but if you want to derive from me, you can't!".
As other posters pointed out, it cannot be applied to non-virtual functions.
One purpose of the final keyword is to prevent accidental overriding of a method. In my example, DoStuff() may have been a helper function that the derived class simply needs to rename to get correct behavior. Without final, the error would not be discovered until testing.
Supplement to Mario Knezović 's answer:
The above code shows the theory, but not actually tested on real compilers. Much appreciated if anyone paste a disassembled output.
Final cannot be applied to non-virtual functions.
It wouldn't be very meaningful to be able to mark a non-virtual method as 'final'. Given
a->foo()
will always callA::foo
.But, if A::foo was
virtual
, then B::foo would override it. This might be undesirable, and hence it would make sense to make the virtual function final.The question is though, why allow final on virtual functions. If you have a deep hierarchy:
Then the
final
puts a 'floor' on how much overriding can be done. Other classes can extend A and B and override theirfoo
, but it a class extends C then it is not allowed.So it probably doesn't make sense to make the 'top-level' foo
final
, but it might make sense lower down.(I think though, there is room to extend the words final and override to non-virtual members. They would have a different meaning though.)
A use-case for the 'final' keyword that I am fond of is as follows:
final
adds an explicit intent to not have your function overridden, and will cause a compiler error should this be violated:As the code stands, it compiles, and
B::foo
overridesA::foo
.B::foo
is also virtual, by the way. However, if we change #1 tovirtual int foo() final
, then this is a compiler error, and we are not allowed to overrideA::foo
any further in derived classes.Note that this does not allow us to "reopen" a new hierarchy, i.e. there's no way to make
B::foo
a new, unrelated function that can be independently at the head of a new virtual hierarchy. Once a function is final, it can never be declared again in any derived class.