Paul Licameli
2016-08-10 22:48:09 UTC
James lately commented in a commit:
(Cosmetic) added 'final' to class definition.
Putting the word 'final' is a bit like adding a comment that nothing is
derived from this effect.
My understanding is that final can cause very slightly more efficient code
to be generated. If Y derives from X, and the inheritance is final, and
class Y has a virtual function, and you call that virtual function through
a pointer-to-Y variable, then the compiler can deduce that it does not need
to generate the code to do the virtual function dispatch, but can jump to
Y's override directly.
Not the compelling reason to use final, but it does make final a little
more than a comment. Yet still I think final is mostly useful as a comment
on the programmer's intention.
Whereas const and override are more than mere comments: they help the
compiler to help you, by restricting what you can legally write. Pass
compilation checks with const and override in place, and you have more
confidence that the code is correct. override in paticular, a new C++11
thing, prevents you from getting the argument or return types wrong in an
intended virtual function override -- which would otherwise quietly compile
without override, but not do what you intended.
PRL
(Cosmetic) added 'final' to class definition.
Putting the word 'final' is a bit like adding a comment that nothing is
derived from this effect.
My understanding is that final can cause very slightly more efficient code
to be generated. If Y derives from X, and the inheritance is final, and
class Y has a virtual function, and you call that virtual function through
a pointer-to-Y variable, then the compiler can deduce that it does not need
to generate the code to do the virtual function dispatch, but can jump to
Y's override directly.
Not the compelling reason to use final, but it does make final a little
more than a comment. Yet still I think final is mostly useful as a comment
on the programmer's intention.
Whereas const and override are more than mere comments: they help the
compiler to help you, by restricting what you can legally write. Pass
compilation checks with const and override in place, and you have more
confidence that the code is correct. override in paticular, a new C++11
thing, prevents you from getting the argument or return types wrong in an
intended virtual function override -- which would otherwise quietly compile
without override, but not do what you intended.
PRL