"C++" namespace

Namespace concept

When you first learn C++, the first piece of code you will write is definitely this:

#include<iostream>
using namespace std;

int main()
{
    
    
	cout<<"hello world"<<endl;
	
	return 0;
}

I believe that most beginners still don’t know the meaning of the first two lines of code. In fact, I didn’t know at the beginning. The teacher said to write like this, just remember it. Then I gradually figured it out. The first line is easy to understand. It is the standard input and output header file. The focus is on the second line of code. To be honest, I really didn’t know what this line of code is for at the beginning. Understand, so every pit is actually a step for one's own progress.

The knowledge involved in this line of code is called namespace, how to understand namespace, first look at the following code:

#include<iostream>
using namespace std;

int main()
{
    
    
	int cout = 0;
	cout << cout << endl;

	return 0;
}

The result of the operation is directly dead. In C++, there are a large number of variables, functions and classes. The names of these variables, functions and classes all exist in the global scope, which may cause a lot of conflicts. The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts and name pollution.
Refine some key information:

  • Namespace is used to avoid naming conflicts or name pollution
  • Keywords: namespace
  • Both variables and functions can be defined

Namespace definition

  • Common definition

To define a namespace, you need to use the namespace keyword.
First define a common namespace Nanlin_1

namespace Nanlin_1
{
    
    
	int a; //定义变量
	int Add(int b,int c)//定义函数
	{
    
    
		return b+c;
	}
}
  • Namespaces can also be nested
namespace Nanlin_2
{
    
    
	int a; 
	int Add(int b,int c)
	{
    
    
		return b+c;
	}
	namespace Nanlin_3
	{
    
    
		int d; 
		int Sub(int e,int f)
		{
    
    
			return e+f;
		}
	}
}
  • There can be multiple namespaces with the same name in the same project, and the compiler will eventually combine them in one namespace
  • One thing to note is that a namespace actually defines a new scope, and everything in the namespace is limited to this namespace.

Namespace usage

There are three ways to use the namespace, which are explained by code descriptions.
Use the namespace Nanlin uniformly.

namespace Nanlin
{
    
    
	int a=1; 
	int b=2;
}
  • Using namespace Nanlin
    and using namespace std are the same, this is to expand the C++ standard library, cout, cin, etc. can be used directly, see the results of the code below, you can directly use a and in the main function b.
    Insert picture description here
    To sum up, the advantage of this method of use is convenience, but it brings naming pollution

  • using Nanlin::a

This is to put the a in the namespace separately, you can use it casually.
Insert picture description here
This time if you use b again, you will get an error. If you only use a, you can run it, but if you want to use b, how to use it, see the third method of use

Insert picture description here
To sum up, this method is to release a certain variable or function in the namespace separately.

  • Nanlin::a, Nanlin::b

One of the above two types is to expand all of them for casual use, and the other is to expand one individually. This is to not expand and specify the access.

Insert picture description here
If I want to use a, I specify access to a, and if I want to use b, I specify access to b

Of course, the above three methods of use can be mixed

Guess you like

Origin blog.csdn.net/NanlinW/article/details/102919380