c++ namespace

 Namespaces:

concept:

In fact, it is a memory area named by the programmer. The programmer can specify some named space fields as needed, and put some global entities in each namespace, so as to separate them from other global entities. In fact, it is proposed to solve the function and variable conflict. A namespace can contain the following contents: (Not only variables but also the following types can be included in curly braces) 
Variables (which can be initialized); 
Constants; 
Numbers (which can be definitions or declarations); 
Structures ; 
Classes  ;
Templates 
_

1  namespace nsl 
 2  {
 3   const  int RATE= 0.08 ; // constant 
4 double pay; // variable 
5  double tax() // function 
6 { return a* RATE;} 
 7  namespace ns2 // nested namespace 
8 { int age; } 
 9 }

 

 Ways to use namespaces:

You can use the scope resolution symbol: : to use variables, functions and classes in the namespace (actually, it is equivalent to, for example, that there are Xiaoming in both the first and second classes of the grade. When the grade-level meeting is called, only add Xiaoming in front of it. It can only be clear to the supervisor), but it is a bit cumbersome to do this every time, so we can simplify this behavior with using declarations and using pragmas. using declaration: using ns1::pay; after the declaration, we can directly use the pay variable; the using compilation directive refers to: using namespace ns1; tell the compiler that all elements of this namespace can be used by me, which is a very lazy approach. Elements defined in namespace must be used in the above three ways! In the general case we declare it in the header file and define it in the source file.

It can be seen from the above that namespaces can be nested, that is, namespaces can be defined in namespaces, and using declarations and using compilation directives can be used in namespaces. It should be noted here that using compilation directives can be passed. If using using in A The compilation instruction uses B. If there is code that uses the using compilation instruction to use A, then B also uses the using compilation instruction at the same time. Not only that the header file uses the using directive to use B, but at the same time B also uses the using directive. Namespaces can be discontinuous, which means that a namespace can be supplemented in multiple files.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325121496&siteId=291194637