C ++ namespace/C ++ namespace

Source: Internet
Author: User

0. The preface namespace is a method provided by C ++ to solve the symbol Name Conflict. A command space is a scope. The same symbols in different namespaces represent different entities. Generally, the namespace method can facilitate the division of modules and reduce the interaction between modules. 1. a namespace member is defined as a namespace member in the namespace. The name in the namespace can be directly accessed by other Members in the namespace. The code outside the namespace must specify the namespace in which the name is located. A namespace can contain multiple types of identifiers, as listed below: variables, constants, functions, struct/consortium/enumeration, classes, and reference methods for nested namespace members are as follows: namespace_name: member_name 2. Define namespace (1). A namespace can be defined in two places: it is defined at the global scope level or in another namespace (this forms a nested namespace) and cannot be defined within functions and classes. (2) The namespace can be discontinuous. It is composed of the whole of all the parts defined by separation. A namespace can be divided into multiple files. Definitions of namespaces in different files are also cumulative. Usually, the namespace declaration is put into the header file, and the implementation is put into the source file. You can put irrelevant members in different header files. (3) The scope of the command space cannot end with a semicolon. 3. Nested namespace (nested Namespce) 3.1 and general Nested namespace (ordinary nested namespace) A Nested namespace is a nested scope with its scope Nested in its namespace. When externally referencing members in a nested space, use the following format to include the name of the namespace of the nested space :: the following example shows how to define a nested namespace and use the copy Code # include <iostream> namespace MyOutNames {int iVal1 = 100; int iVal2 = 200; namespace MyInnerNames // defines the nested namespace {int iVal3 = 300; int iVal4 = 400 ;}} int main (void) {std: cout <MyOutNames :: iVal1 <std: endl; std: cout <MyOutNames: iVal2 <std: endl; std: cout <MyOutNames: MyInnerNames :: iVal3 <std: endl; // use the nested namespace member std: co Ut <MyOutNames: MyInnerNames: iVal4 <std: endl; // use the nested namespace member return 0 ;} inline Namespace is added to copy code 3.2 and inline nested namespace (Inline Namespace C ++ 11) C ++ 11, indicates that the namespace name is directly included in the outer namespace. This facilitates version management of namespaces and reduces conflicts. The inline descriptor makes the declarations in the inline namespace look like they are directly declared in the peripheral namespace. (The inline namespace defined using the inline keyword becomes the default namespace .) The inline descriptor is placed by the namespace designer. That is, the creator of the namespace can place the inline descriptor to indicate which namespace is the latest. copy the code // file V98.hpp: namespace V98 {void f (int); // does something //...} // file V99.hpp: inline namespace V99 {void f (int); // does something better than the V98 version void f (double); // new feature //...} // file Mine. hpp: namespace Mine {# include "V99.hpp" # include "V98.hpp"} // file example. cpp # include "Mine. hpp "using namespace Mine ;//... V98: f (1); // old versionV99: f (1); // new versionf (1 ); // default version copy Code 4. Global Namespce defines the name of a Global scope (the name declared outside of any class, function, or namespace) is defined in the global namespace. The global namespace is implicitly declared and exists in each program. In the global scope, each object is defined to add those names to the global namespace. You can use the scope operator to reference members of a global namespace. Because the global namespace is implicit and has no name, use the following method to reference the members of the global namespace.: Member_name 5. The Namespace (Unnamed Namespace) can be untitled. The Namespace is not given a name when it is defined. The definition method is as follows: namespace // No name {// members ....} unlike other namespaces, an unnamed namespace is defined partial to a specific file and never spans multiple text files. An untitled namespace can be discontinuous in a given file, but cannot span the file. Each file has its own untitled namespace. An unnamed namespace is used to declare objects that are partial to a file. Variables defined in an unnamed namespace are created at the beginning of the program and exist until the end of the program. Names defined in Unnamed namespaces can be used directly because they are not defined by namespaces. Copy the Code # include <iostream> namespace // unnamed namespace {int count = 1;} using namespace std; namespace // unnamed namespace {void name_printf (void) {cout <"count =" <count <endl ;}} int main (void) {count = 3; // use name_printf () directly (); // directly use return 0;} to copy the name defined in the namespace not named by the code, which is only visible in the file containing the namespace. If another file contains an unnamed namespace, the two namespaces are irrelevant. You can define the same name, and these definitions reference different entities. The name of an untitled namespace member cannot be the same as that defined in the global scope. The following is an example of a function. Copy the code int I; // global variable namespace // unnamed namespace {int I;} // error: reference to 'I' is ambiguous. Copy the code like other namespaces, an unnamed namespace can also be nested inside another namespace. If the unnamed namespace is nested, the name of the namespace is accessed by using the name of the peripheral namespace in the general method: copy the code int I; // Global Variable namespace local {namespace // unnamed namespace {int I; // OK: I defined in a nested unnamed namespace is distinct from global I} local :: I = 42; copy the Code. If the header file defines an unnamed namespace, the names in the namespace define different local entities in each file that contains the header file. Before using a namespace to replace the static declaration in a file to introduce a namespace in Standard C ++, the program must declare the name as static so that their scope is limited to a file. The static declaration method in the file is inherited from the C language. C ++ disapproves the static Declaration of the file because it may not be supported in future versions. The anonymous namespace provides a function similar to the restriction scope function caused by static modification before the global function. This feature can be used on struct and class, but static cannot modify schema definitions such as class and struct. Therefore, we should avoid static files and use unnamespace instead. 6. A namespace alias can be used to create an alias for a namespace. An alias is a replaceable name for a defined namespace. A namespace can have many aliases, and all aliases and the original namespace names can be used interchangeably. You can create a namespace alias by specifying the alias to the name of the defined namespace in the following format. Namespace alias = Name of a defined namespace. The following example describes the definition of a namespace alias and how to use the copy Code # include <iostream> namespace MyNames {int iVal1 = 100; int iVal2 = 200;} namespace MyAlias = MyNames; // alias defines int main (void) {std: cout <MyAlias: iVal1 <std: endl; // use std: cout for the alias <MyAlias: iVal2 <std: endl; // use return 0 for the alias ;} copy code 7, using declaration, and using indication the advantage of using declaration and using indication is that when using a namespace member, you do not need to add a namespace scope. Using std: cout; // using declares using namespace std; // using indicates 7.1, using declares (using declaration) A using declares that only one namespace member is introduced at a time. The using declaration scope starts from the using declaration point until the end of the scope containing the using declaration, and the name is visible. Entities with the same name defined in the external scope are blocked. The using declaration can appear in the global, local, Class scope, and namespace. In the class scope, the using Declaration can only reference base class members. Copy the code // using declaration in Global Scope # include <iostream> using std: cout; // using declares using std: endl; // using declares int main (void) {cout <"Hello World" <endl; return 0;} copy the code and copy the code // using declaration in Local Scope # include <iostream> void func (void) {using std: cout; using std: endl; cout <"Using Declarations In Function" <endl ;}int main () {func (); // cout <"Hello" <endl; // error: 'cout' and 'endl' Were not declared in this scope return 0;} copy the code and copy the code // using declaration in Class Scope # include <stdio. h> class B {public: void f (void) {printf ("In B: f () \ n");} void g (void) {printf ("In B: g () \ n") ;}}; class C {public: void g () {printf ("In C: g () \ n ") ;};}; class D: public B {public: using B: f; // OK: B is a base of D2 using B: g; // OK: B is a base of D2 // using C: g; // error: C isn't a base of D2 }; Int main (void) {D MyD; MyD. f (); MyD. g () ;}copy the code and copy the code // using declaration in Namespce # include <iostream> namespace MyNames {using std: string; using std: cout; using std :: endl; string str; void func (void) {cout <"Hello" <endl ;}} int main (void) {MyNames: func (); return 0 ;} copy code 7.2, using directive (using directive) using directive to make the entire namespace member visible. Using indicates that it can appear in global and local scopes and namespaces, but not in the scope of the class. Copy the code // using directive in Global Scope # include <iostream> using namespace std; // using indicates int main (void) {cout <"Hello World" <endl; return 0;} copy the code and copy the code // using directive in Local Scope # include <iostream> void func (void) {using namespace std; cout <"Using Declarations In Function" <endl ;}int main () {func (); // cout <"Hello" <endl; // error: 'cout' and 'endl' were not declared in this scope return 0;} copy the code and copy the code // using declaration in Namespce # include <iostream> namespace MyNames {using namespace std; string str; void func (void) {cout <"Hello" <endl ;}int main (void) {MyNames: func (); // cout <"Hello" <endl; // error: 'cout' and 'endl' were not declared in this scope return 0;} copy code 7.3, avoid using to indicate using to inject all names from a namespace, this method seems simple, but if the application uses many libraries and using instructions to make the names in these libraries visible, the global namespace pollution issue will reappear. Compared with the using instruction, it is better to use the using Declaration for each namespace name used in the program. In this way, the number of names injected into the namespace is reduced, ambiguity errors caused by using declarations are easy to detect and correct. 8. Copy the code using the integrated application example /// file: mynames. hpp # ifndef MYNAMES__HPP _ # define MYNAMES__HPP _ namespace MyNames {// Member: Variable extern int iVal; // Member: Class class MyString {public: MyString (const std :: string &); void OutputString (void); private: std: string str ;}; // Member: Function void NormalFunc (void); // Member: struct struct st_Names {char ch; int count ;}; // Member: Union union Union un_Names {char ch; int count ;}; // Member: enum en_Names {ZERO, ONE, TWO };## endif/* MYNAMES__HPP _ * // alias // file: mynames. cpp # include <iostream> # include "mynames. hpp "namespace MyNames {int iVal = 100; MyString: MyString (const std: string & refstr) {str = refstr;} void MyString: OutputString (void) {std:: cout <str <std: endl;} void NormalFunc (void) {std: cout <"NormalFunc" <std: endl ;}} // example // file: example. cpp # include <iostream> # include "mynames. hpp "namespace Name = MyNames; using namespace Name; int main (void) {std: cout <iVal <std: endl; std: cout <Name :: iVal <std: endl; std: cout <MyNames: iVal <std: endl; MyNames: MyString mystr ("Hello"); mystr. outputString (); MyNames: NormalFunc (); MyNames: st_Names myst; myst. count = 0; MyNames: en_Names myen; myen = MyNames: ZERO; MyNames: un_Names myun; myun. count = 1; return 0 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.