In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how C++ avoids non-constant global variables". In daily operation, I believe many people have doubts about how C++ avoids non-constant global variables. The editor consulted all kinds of data and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how C++ avoids non-constant global variables". Next, please follow the editor to study!
Avoid the non-constant global variable Reason (reason)
Non-constant global variables hide dependencies and cause dependent objects to be modified unpredictably.
Example (sample)
Struct Data {/ /... Lots of stuff...} data; / / non-const data
Void compute () / / don't {/ /... Use data...}
Void output () / / don't {/ /... Use data...}
Translator's note: global variables can be seen by any code and can be modified by any code. If there are no special rules for variable naming, this problem will be more difficult to solve.
Note (Note)
Global constants are useful.
Note (Note)
The rules for global variables also apply to namespace-scoped variables.
Optional: if you use global (and more generally, namespace-scoped) data to avoid copying, consider passing it through a reference of type const. Another solution is to define the data as the state of some objects and define the operation as a member function.
Translator's note: in the first case, the code referenced by const cannot rewrite the contents of the global variable, so that modifications to the global variable can be restricted; in the second case, changes to the global data can be controlled and checked by member functions.
Warning: beware of data competition. If two threads access the same data (either globally or by reference), data contention may occur. Data contention can occur for every pointer or reference to modifiable data.
Note (Note)
You can't make unmodifiable data compete for data.
Note (Note)
This rule is "avoid" rather than "prohibit use".
Note to the translator: "avoid" can be understood as not using it if possible.
Enforcement (implementation recommendations)
(simple) reports all non-constant variables defined in the namespace scope.
At this point, the study of "how C++ avoids non-constant global variables" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.