C++ Fundamentals-Initial Explanation of Namespaces

Why do you need a namespace

Suppose there is a program that simulates the scene of a class roll call, and there are students with the same name in the class. At this time, the teacher will definitely not call the name directly, but will add some signs or characteristics to the name of the same name to show the difference.
Insert picture description here
The program is a clumsy one, it will only roll out the names step by step according to the pre-settings. It will not be dealt with urgently because there are students with the same name (add a modification to distinguish, etc.).

It can be roughly understood that the teacher here is to compile the rules, and the students are variables. This kind of name conflict problem is called 命名空间污染问题 1 .

For large projects, a project often needs to be divided into independent sub-modules and then merged.

And the naming commonly used by most people is easy to be the same.

C++Provides us with another method, namely 命名空间.

how to use

In the above example, the namespace is used as follows:

//老刘写的
namespace Liu{
    
     
	int id;
    //...
    void fun();
}
//老郭写的
namespace Guo{
    
    
    int id;
    //...
    void fun();
}

::Resolution operator domain in C++the namespace specified to be used.

Therefore, when used, it is: Liu::idGuo::id

Note :
namespace may be defined in other internal global scope or scopes, but not inside the class definition or functions 1 .

Not only can the internal namespace declaration or definition of variables for which the name or other declarations can be defined outside of the namespace, also can be declared or defined inside a namespace, such as classes, functions typedef, #defineand so can appear in a namespace 2 .

Using

In C++we often see std, it is actually the namespace of the standard library.

std::cout << "Hello Word ! " << std::endl;

The above is a flag output. If you add a namespace every time you use the standard library, it is extremely cumbersome.

After all, being lazy is a big motivation for programmers.

Insert picture description here

C++Provides a more concise way to use namespace members, ie using.

The instruction tells the compiler, code behind, set to the default namespace usingspecified namespace, then, the above code can be rewritten as:

using namespace std;

cout << "Hello Word ! " << endl;

In addition, you usingcan also target a variable/method

using namespace Liu::id;

From the perspective of compilation and linking, variable names, function names, and class names appearing in the code are all symbols. Some symbols may refer to a memory location, for example, variable names, function names; some symbols is just a new name, for example, typedefthe type of alias definition 2 .

Discrete namespace

Unlike scope, namespaces can be discontinuous. The namespace is composed of the sum of its separate definition parts, and the namespace is cumulative 1 .

We can A文件increase in 命名空间A, in B文件the same use 命名空间A, and add some content.

namespace Liu{
    
     
	int id;
    //...
    void fun();
}

That is, the above code can either define a new namespace or add it to an existing namespace.

It is worth noting: If a part needs to request namespace name defined in another file, you still need to declare the name 3 .

Nested namespace

Namespaces can be nested, and you can define one namespace in another, as shown below:

namespace namespace_name1 {
    
    
   // 代码声明
   namespace namespace_name2 {
    
    
      // 代码声明
   }
}

Use as follows:

// 访问 namespace_name2 中的成员
using namespace namespace_name1::namespace_name2;
 
// 访问 namespace:name1 中的成员
using namespace namespace_name1;

Reference thanks


  1. 《C ++ Primer》↩︎ ↩︎ ↩︎

  2. C++ namespace (name space) detailed explanation ↩︎ ↩︎

  3. Novice Tutorial-C++ Namespace ↩︎

Guess you like

Origin blog.csdn.net/weixin_40774605/article/details/105253514