GoingNative 26: The final* Episode

Play GoingNative 26: The final* Episode

The Discussion

  • User profile image

    Would love to see more on C++14.  Thanks.

  • User profile image

    A good explanation of the benefits of 'final'.

    This inspired me to double-check my C++/CLI code and I see a similar benefit if I use 'sealed' for my C++/CLI classes. Thanks for the nudge to try this Patrick and Gabriel!

  • User profile image

    @schroedl:=) Glad we could help!

  • User profile image

    Still no good reasons given to why would anybody need to declare a class or a virtual function 'final'. The 'final' actually looks like a misfeature in C++11.
    Firstly, from a design perspective, it's a misplaced responsibility. The decision of whether to derive or not from a class (or override a virtual function) resides on the consumers of the class, not the class itself. The class cannot and shouldn't know whether it's final.
    Secondly, there is a question of optimization. Can it actually be helped by 'final'? -- Not really. The proper OO design assumes the call of the derived objects via the base pointer: Liskov Substitution Principle. In which case, for a compiler to eliminate the virtual call it needs to know the dynamic type of the object. Whether compiler is able to that in a particular situation doesn't depend on the 'final'.

  • User profile image
    Patrick Sathyanathan

    With proper use of "final" the developer can tell the compiler that a specific method will be called irrespective of the dynamic type of the object. The analysis to determine the latter is in general complex and expensive. "final" provides a way to get the benefit of that analysis without having to do it and does not violate the Liskov Substitution Principle.
    Whether the keyword "final" is necessary for robust and extensible C++ libraries (and programs) is a matter of discussion in the C++ community.
    The keyword "final" allows programmers to directly express aspects of their design. Like any good language feature, its effective use requires careful analysis and experience. For analysis, please consider this FAQ entry http://www.stroustrup.com/C++11FAQ.html#final

Add Your 2 Cents