C++ basic syntax - namespace

Foreword: C++, cplusplus, as the name suggests, is the promax version of C language. C++ is compatible with C language.
C++ was born because Benjani and other big guys at Bell Labs believed that the C language had too many grammatical pitfalls and had many shortcomings (such as naming conflicts), which caused inconvenience after long-term use, so C++ was invented. C++ to fill in these syntax holes.

Namespaces

Before understanding the namespace, the blogger first asked a small question: We often include packages at the beginning of C++ files using namespace std;, so do you know what the function of this sentence is? (Clip with yellow background below)

There is a naming conflict problem in C language. If you and your colleagues collaborate on a project in the future, naming conflicts may occur when the libraries you are responsible for are linked. For example, if you declare a variable with the same name, in C language , at this time only one of you can make a concession and change all the variables to aliases. The C++ namespace was born to solve this situation.

1. Namespace and scope qualifiers::

namespace example  
{
    
    
	//命名空间中可以定义变量/函数/类。
	int a = 0;
	int Add(int x,int y){
    
    
		return x+y;
	}
}
int main()
{
    
    
	//可以将命名空间想象成一个围墙,围墙里面的东西必须要有权限才能访问。
	//cout << a << endl; 实操后发现并不能打印出a的值
	cout << example::a << endl; //域作用限定符::  借助该符号可以访问命名空间里的数据。
	return 0;
}

2. Expand the namespace

1. Partial expansion (authorization)

Suppose we need to frequently use the Add function in the namespace created above, but it is particularly cumbersome to use the domain scope qualifier every time. This is where we can expand part of the namespace.
The format is: using example::Add;

namespace example
{
    
    
	int a = 0;
	int Add(int x,int y){
    
    
		return x+y;
	}
}
using example::Add;   //展开部分命名空间
int main()
{
    
    
	cout << Add(1,2) << endl; //这里并没有运用与操作限定符,却也可以使用命名空间里的Add函数。
	return 0;
}

2. Expand all

Format: using namespace example;
std is the namespace of the C++ standard library. Using namespace std; is to expand the entire C++ standard library. If std is not expanded, then cout, cin and other syntaxes cannot be used.

namespace example
{
    
    
	int a = 0;
	int Add(int x, int y) {
    
    
		return x + y;
	}
}
using namespace example;  //全部展开命名空间
int main()
{
    
    
	//不用域作用限定符也可访问example里的变量,函数
	cout << a << endl;
	cout << Add(1, 2) << endl;
	return 0;
}

3. Matryoshka doll

Namespaces can be nested within namespaces, but they are generally not used this way, but don’t be surprised if you see them accidentally.

namespace example
{
    
    
	int a = 0;
	namespace instance
	{
    
    
		int a = 1;
	}

int main()
{
    
    
	cout << example::a << endl;  // 0
	cout << example::instance::a << endl; // 1
	return 0;
}

Summary: In summary, there are three ways to use namespaces.
If you want to define two or more variables with the same name in the future, you can place them in different namespaces.
Using namespaces well will save you a lot of unnecessary trouble in future exercises and work.

BB at the end of the article: For friends who have any questions, feel free to leave a message in the comment area. If there are any questions, friends are welcome to point out in the comment area. The blogger will confirm the modification as soon as possible after seeing it. Finally, it is not easy to make. If it is helpful to my friends, I hope it will make you rich and give me some likes and attention.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/132136265