In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what do you need to pay attention to when C++ uses final". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what do you need to pay attention to when C++ uses final?"
Use final cautiously
Reason (reason)
Capping a hierarchy with final is rarely needed for logical reasons and can be damaging to the extensibility of a hierarchy.
It is rare to use the final keyword to turn off subsequent override functions for logical reasons, which breaks the extensibility of inheritance.
Example, bad (negative example) class Widget {/ *... * /}
/ / nobody will ever want to improve My_widget (or so you thought)
Class My_widget final: public Widget {/ *... * /}
Class My_improved_widget: public My_widget {/ *... * /}; / / error: can't do that
Note (Note)
Not every class is meant to be a base class. Most standard-library classes are examples of that (e.g.std::vector and std::string are not designed to be derived from). This rule is about using final on classes with virtual functions meant to be interfaces for a class hierarchy.
Not all classes are designed as base classes. Most of the classes in the standard library are examples of this (for example, std::vector and std::string are not designed to inherit). The scope of this rule is for classes that contain virtual functions and are intended to be inherited as interfaces.
Note (Note)
Capping an individual virtual function with final is error-prone as final can easily be overlooked when defining/overriding a set of functions. Fortunately, the compiler catches such mistakes: You cannot re-declare/re-open a final member ina derived class.
When defining / overriding a set of functions, finial is easily ignored, and using final to turn off the override function for each individual virtual function can easily lead to an error. Fortunately, the compiler can catch these errors: you cannot redefine or reopen a final member in a derived class.
Note (Note)
Claims of performance improvements from final should be substantiated. Too often, such claims are based on conjecture or experience with other languages.
There is a lack of evidence that using final can improve performance. In too many cases, this judgment is only based on speculation or experience in other languages.
There are examples where final can be important for both logical and performance reasons. One example is a performance-critical AST hierarchy in a compiler or language analysis tool. New derived classes are not added every year and only by library implementers. However, misuses are (or at least have been) far more common.
There are scenarios where final becomes important, whether for logical or performance reasons. One example is a compiler or language analysis tool that requires very strict performance. Only the implementers of the library will add new derived classes, and will not add them every year. But misuse is more common (at least it has been misused).
AST:Abstract syntax tree (Abstract Syntax Tree)-translator's Note
Enforcement (implementation recommendations)
Flag uses of final.
Flag for the use of final.
Thank you for your reading, the above is the content of "what do you need to pay attention to when C++ uses final"? after the study of this article, I believe you have a deeper understanding of what you need to pay attention to when C++ uses final, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.