Namespaces: Concepts:
is actually a memory area named by the program designer, which can be separated from other global entities by assigning some names to the domain and placing some global entities in each namespace, respectively. In fact, it is to solve the function, variable conflict and proposed. A namespace can contain several things: (you can include not only variables in curly braces, but also the following types)
• Variables (can be initialized);
constant
• Number (can be defined or declared);
• Structural body;
Class
Template
• Namespaces (a namespace is defined in a namespace, which is a nested namespace ).
1 namespaceNSL2 {3 Const intRate=0.08;//Constants4Double pay;//variables5 DoubleTax ()//function6{returnA *Rate ;}7 namespaceNs2//nested namespaces8{intAge ;}9}
Ways to use namespaces:
You can use the scope to resolve the symbol: to use the variables under the namespace, functions and classes and so on (in fact, the equivalent of Grade one and class two have a small Ming, the opening of the grade meeting, only in front of Xiao Ming plus class can be divided clearly), but each time it will be a little trouble, So we can simplify this behavior with using declarations and using compiler directives. Using declaration: Using ns1::p ay; we can use the pay variable directly after the declaration, using a compiler directive means: using namespace ns1; Tell the compiler that all the elements of this namespace I can use, which is a very lazy approach. The elements defined in namespace must be used in the three ways above! in general, we declare in the header file, defined in the source file.
The namespace can be nested, that is, you can define namespaces in a namespace, you can use using declarations and using-compilation directives in namespaces, and it is important to note that a using- compiler directive is transitive, and if you use a using-compiler directive for B in a, If you have code that uses a using compiler directive, then B is also using the using compiler directive. Not only that the header file uses the using-compile instruction with B, but at the same time B also uses the using-compiler directive. Namespaces can be discontinuous, meaning that a namespace can be complemented in multiple files.
C + + namespaces