Summary of C++ namespace knowledge points

introduce

in a C++ application. One might write a function called xyz() and an identical function xyz() exists in another available library. This way, the compiler cannot tell which xyz() function is used.

Therefore, the concept of namespace was introduced specifically to solve the above problems. It can be used as additional information to distinguish functions, classes, variables, etc. with the same name in different libraries. Using a namespace defines a context. Essentially, a namespace defines a scope.

Let's take an example in a computer system. A folder (directory) can contain multiple folders. Each folder cannot have the same file name, but files in different folders can have the same name.

,

Namespaces

definition

A namespace is defined using the keyword namespace, followed by the name of the namespace, as follows:

namespace namespace_name {
// 代码声明
}

transfer

In order to call a function or variable with a namespace, you need to add the name of the namespace in front. When specifying the function or variable used, you need to use the "::" operator. The "::" operator is the domain resolution operator. . As follows:

name::code;  // code 可以是变量或函数 

Example

#include<iostream>
using namespace std;

namespace myName
{
    int x=5;
}

int main()
{
    int x=1;
    cout<<x<<endl;
    cout<<myName::x<<endl;
    return 0;
}

The output is:

1

5

using directive

In addition to directly using the domain resolution operator::, you can also use a using declaration (using declaration)

Example 1 (commonly used)

You can use the using namespace directive so that you do not need to prefix the namespace name when using the namespace. This directive tells the compiler that subsequent code will use names in the specified namespace.

#include <iostream>
using namespace std;
 
// 第一个命名空间
namespace first_space{
   int a;
   void func(){
      cout << "Inside first_space" << endl;
   }
}
// 第二个命名空间
namespace second_space{
   int a;
   void func(){
      cout << "Inside second_space" << endl;
   }
}
using namespace first_space;
int main ()
{
 
   // 调用第一个命名空间中的函数
   func();
   
   return 0;
} 

When the above code is compiled and executed, it produces the following results:

Inside first_space 

Example 2

The using directive can also be used to specify specific items in a namespace. For example, if you only plan to use variable a in the first_space namespace, you can use a statement like the following:

using first_space::a; 

In subsequent code, you do not need to prefix the namespace name when using the variable a of the first_space namespace, but other items in the first_space namespace still need to be prefixed with the namespace name.

Summarize

The C++ standard namespace is std, which contains many standard definitions. All variables, functions, etc. defined in the header file iostream file are located in the std namespace. It is very troublesome to add std:: every time you use a variable or function in iostream. For this, you can directly use The using statement declares all variables or functions in std. If you do not use the using namespace std; statement, the program should look like this:

#include<iostream>
int main(){
    std::cout<<"hello world!"<<std::endl;
    return 0;
} 

After using the using namespace std; statement, the program should look like the following:

#include<iostream>
using namespace std;
int main(){
    cout<<"hello world!"<<endl;
    return 0;
} 

Nesting of namespaces

Namespaces can be nested, and one namespace can be defined within another namespace, as shown below:

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

Members of nested namespaces can be accessed by using the :: operator:

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

Guess you like

Origin blog.csdn.net/m0_73879806/article/details/134279822