In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, the editor will bring you about what is the basic namespace of C++ language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The use of C++ namespaces
The C++ language introduces the concept of namespace (Namespace) mainly to avoid naming conflicts, and its keyword is namespace.
With the development of science and technology, a system is usually not developed by only one person. When different people develop the same system, there will inevitably be conflicts in the naming of variables or functions. When everyone's code tests pass and there is no problem, combining everyone's code together will cause some confusion due to the duplicate names of variables or functions, such as:
Int flag = 1; / / the variable declared by Xiao Li / /... / / interval the variables declared by several lines of code bool flag = true; / / Xiao Han
Note: this example is only used to explain namespaces, which is not the case in the company's system development, and completely relies on namespaces to resolve naming conflicts. Specific programming specifications can refer to Lin Rui's "High-quality programming Guide".
As shown above, because of his different personal habits, Xiao Li likes to declare int variables for logical judgment, while Han prefers to use bool type variables. But when two declarations are placed in the same function, it is obvious that the compiler will prompt an error in the redefinition of the flag variable. This kind of problem cannot be compiled if it is not dealt with.
You can use namespaces to resolve naming conflicts similar to the above, such as:
Namespace Li {/ / Xiao Li's variable declaration int flag = 1;} namespace Han {/ / Xiao Han's variable declaration bool flag = true;}
Xiao Li and Xiao Han have respectively defined namespaces with their surnames. At this time, there will be no problem if the definitions of the flag variables of Xiao Li and Han are put in the same function body. Of course, when using these two variables, you need to indicate which namespace flag variable is used.
The "::" operator is required to specify the variables to be used, and the "::" operator is a domain resolution operator. For example:
Li::flag = 0; / / use the variable flagHan::flag = false; defined by Xiao Li / / use the variable flag defined by Xiao Han
We have defined two namespaces Li and Han, and declare the flag variable in each of them. When using it, we need to use the domain resolution operator to indicate who defines the flag variable at this time, and whether it is defined by Xiao Han or Xiao Li.
In addition to using the domain resolution operator directly, you can also use a using declaration (using declaration), such as:
Using Li::flag;flag = 0; / / use the variable flagHan::flag = false; defined by Xiao Li / / use the variable flag defined by Xiao Han
The Li::flag is declared with using at the beginning of the code, which means that using declares that if there is an unspecified flag in later programs, then use Li::flag, but if you want to use the flag defined by Han, you still need Han::flag.
Using declarations can be used to declare not only one variable in a namespace, but also an entire namespace, for example:
Using namespace Li;flag = 0; / / use the variable flagHan::flag = false; defined by Xiao Li / / use the variable flag defined by Xiao Han
If other variables are defined in the namespace Li, it will also have the effect of flag variables. After using declaration, if there are naming conflict variables that do not specify a namespace, the variables in the Li namespace will be used by default. Not only variables can be declared or defined within a namespace, but other entities that can be declared or defined outside the namespace can also be declared or defined within the namespace, such as variable declaration or definition, function declaration or definition, typedef, and so on.
Let's take a look at an example of a simple C++ program:
# includeusing namespace std;int main () {cout
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.