Chapter One namespace namespaces

Source: Internet
Author: User

First, what is namespace?

Namesapce is a form of control that is provided to prevent name collisions.

When a program needs to use a lot of library files, name collisions are sometimes unavoidable. The previous solution is to use a longer variable name, which is inconvenient to use.

Ii. Definition of namespaces

1.namespace space name {

Variable

function

Class

}

Three, the characteristics of the name space

1, each namespace has its own scope;

2, the namespace can be discontinuous, just as the example in the code enumerated;

1.1 How to understand the scope of namespaces

1 namespaceglobal_namespace{2 3 4              namespacelocal_namespace_1{5 6               inti;7               intJ;8              }9 Ten  One              namespacelocal_namespace_2{ A  -               inti; -               intJ; the              }    -  -  -  +}

Line6~7 and line13~14 all define the same variables, but do not conflict;

Because the references were

global_namespace::local_namespace_1:: i=10;
Global_namespace::local_namespace_2:: i=10;

In use, you need to explicitly indicate the space of the variable, because it will not conflict! But this kind of writing, see there is a collapse of the bright.

Fortunately, applications within the same namespace don't need to be so verbose.

 1  namespace                                local_namesapce_3{ 2  int  i=10   3  10  ;  4  6  int  sum = I+J; 
     
      //here is the 
       7  8  9 } 

The 1.2 namespace can be discontinuous , how to understand it?

When we define a namespace, there are two possible scenarios:

1) has never been defined before, here is the first time to define a namespace;

2) Previously defined, this means adding some new members to the original namespace

Here is an example of the second case:

Namespaces are defined in the AA.HH header file

namespace   namespace_test{int  i; int J;}

Then we define a namespace with the same name in the BB.HH header file.

namespace   namespace_test{int  i; int J;}

Compile to see what surprises will happen ....

Error:redefinition of ' int namespace_test::i '

Error: ' int namespace_test::i ' previously declared here

Compiled results, it can be shown that, although the namespace is defined in separate files, the space is discrete, but the compiler still think that the two are in the same namespace, so the variable is given a duplicate definition of the error!

OK, the characteristics of the namespace analysis of the temporary here, after the encounter will continue to further add .....

Iv. Types of namespaces

1. Global namespace (Globals namespace)

2, inline namespaces (inline namesapce)

3. Unnamed namespaces (unnamed namespace)

$$4.1 understand what a global namespace is

The book says that the global namespace is declared in an implicit way, what is implicit? That is, even if you do nothing, your code will always float like a global net, and the variables, functions, and classes you define, declare, are shrouded in it. Of course, for this invisible net, it is not a little can not touch. The variables inside of it can be referenced in the following ways:

: Members because the implicit global namespace has no name, so it can only be called by the unsung heroes.

/************************************************************************************************************** */

$$4.2 understand what an inline namespace is

Defined:

Inline namespace space Name {

}

This is a new feature of c++11.

/************************************************************************************************************** */

$$4.3 understand what an unnamed space is

Chapter One namespace namespaces

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.