C + + Namespace instance parsing _c language

Source: Internet
Author: User

Namespace is a very important concept of C + +, this article in the form of an in-depth analysis of the case, the specific contents are as follows:

Typically, in C + +, the purpose of a namespace (namespace) is to prevent name collisions. Each namespace is a scope, and in addition to all namespaces, there is a global namespace (global namespace), which is declared implicitly, and does not have a name. In the namespace mechanism, the original global variable is in the global namespace (can be represented as:: member).

First, define namespaces

1, each namespace is a scope

Like other scopes, each name in a namespace must represent a unique entity, whereas in different namespaces you can have members of the same name .

2, the namespace can be discontinuous

Namespaces can be defined in several different parts:

namespace NSP {/ 
  * ...  /}//Namespace scope after no semicolon

If there was no namespace definition named NSP, the code above creates a new namespace, otherwise the code above opens an existing namespace to add some new members.

3, the namespace can be nested

Nested namespaces are namespaces that are defined in other namespaces. A nested namespace is a nested scope, and the name of the inner namespace declaration hides the member with the same name as the outer namespace declaration:

int x =; 
namespace Outer { 
  int x = ten; 
  namespace inner { 
    int z = x; 
  } 
} 
 
int main () 
{ 
  std::cout << outer::inner::z;//output: 
  0; 
} 

Note that normally we do not place the #include header file inside the namespace .

4, unnamed namespaces

An unnamed namespace (unnamed namespace) is a series of declaration statements that are enclosed in curly braces after the keyword namespace. Variables defined in unnamed namespaces have static lifecycles: they are created before they are first used, and are not destroyed until the program is finished. Before standard C + + introduces the concept of namespaces, a program needs to declare the name to be static to make it valid for the entire file, but now the practice of statically declaring in a file has been canceled by the C + + standard and replaced with an unnamed namespace.

An unnamed namespace can be discontinuous within a given file, but cannot span multiple files. That is, an unnamed namespace is valid only within a specific file and does not span multiple different files. Also, because the unnamed namespace does not have a name, the name defined in it is scoped to the same scope as the namespace:

int i;  Global declaration of I 
namespace { 
  int i; 
} 
i = 10; Error, ambiguity 

Namespace Local { 
  namespace { 
    int i; 
  } 
} 
LOCAL::I = 42; That's right 

Ii. Use of namespaces

A reference to a member in a namespace needs to use the scope operator of the namespace (::). However, it's cumbersome to use namespaces like Namespace_name::member_name, and we need to use some of the other simpler ways.

1, alias name space

Some namespaces have long names or namespaces that are nested in many layers, and we can set a shorter synonym for it, which is the alias:

namespace CLN = Cpluslus_learning_namespace; 
namespace qlib = Outer::inner::querylib; 

2. Using declaration

A using declaration (using declaration) statement introduces only one member of a namespace at a time:

Using namespace name:: [namespace Name:: ...] Member name;  For example using Olib::list; 

3. Using instruction

The difference between the using directive (using Directive) and the using declaration is that we cannot control which names are visible because the using directive makes all the names in a particular namespace visible:

using namespace std;  Introducing a namespace Std 

Namespaces are used primarily to prevent name collisions, and if you arbitrarily use a using directive to inject all the names of namespaces, you will reintroduce the name conflict. In addition, theusing declaration and the using indicator differ in scope : theusing declaration introduces a member into the current namespace scope, and the using indicates that all members are introduced into the current and upper-level namespace scope :

namespace NSP {  //namespace 
  int i=16, j=15; 
} 
 
int j = 0;  global variable 
 
int main () 
{ 
  using namespace NSP;  If J is used, a conflict will occur between: J and Nsp::j 
  Std::cout << J; 
  return 0; 
}

If the using namespace NSP is changed to the using declaration using NSP::J, the correct output is 15.

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.