[C++ basic study notes] understanding and use of namespace namespace


namespace concept

Concept:
In C/C++, variables, functions and classes to be learned later exist in large numbers, and the names of these variables, functions and classes will all exist in the global scope, which may cause many conflicts. The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts or name pollution.
The appearance of the namespace keyword is aimed at this problem.

Simply put, namespaces appear so that the names can be used normally when the names are the same.


Definition of namespace

To define a namespace, you need to use the namespace keyword, followed by the name of the namespace, and then a pair of curly braces { }, where { } is the member of the namespace.

Method 1: Ordinary namespace definition

namespace N1  //N1是命名空间的名称
{
    
    
	//用花括号{ }将命名空间的内容包起来
	//命名空间的内容,即可以定义变量,也可以定义函数
	int a;
	int Add(int x, int y)
	{
    
    
		return x + y;
	}
}

Method 2: Nested Definition of Namespaces

namespace N2
{
    
    
	int a;
	int b;
	int Add(int x, int y)
	{
    
    
		return x + y;
	}
	namespace N3  //命名空间的嵌套定义
	{
    
    
		int c;
		int d;
		int Sub(int x, int y)
		{
    
    
			return x - y;
		}
	}
}

Method 3: Multiple namespaces with the same name are allowed in the same project, and the compiler will finally synthesize them into the same namespace.

namespace N1  //N1是命名空间的名称
{
    
    
	//用花括号{ }将命名空间的内容包起来
	//命名空间的内容,即可以定义变量,也可以定义函数
	int a;
	int Add(int x, int y)
	{
    
    
		return x + y;
	}
}

namespace N1  //再次定义命名空间 N1
{
    
    
	int Mul(int x, int y)
	{
    
    
		return x * y;
	}
}

is actually equivalent to:

namespace N1  //N1是命名空间的名称
{
    
    
	//用花括号{ }将命名空间的内容包起来
	//命名空间的内容,即可以定义变量,也可以定义函数
	int a;
	int Add(int x, int y)
	{
    
    
		return x + y;
	}
	int Mul(int x, int y)
	{
    
    
		return x * y;
	}
}


Examples of the use of namespaces :

#include<iostream>
namespace N
{
    
    
	int a = 10;
	int b = 20;
	int Add(int x, int y)
	{
    
    
		return x + y;
	}
	int Mul(int x, int y)
	{
    
    
		return x * y;
	}
}

int main()
{
    
    
	printf("%d\n", a);//该语句编译出错,无法识别a
	return 0;
}

insert image description here
From the feedback given to us by the compiler, it can be roughly inferred that after defining a namespace, if a name in the namespace is used directly, the compiler cannot actually recognize or find the name.


Correct use of namespaces

Method 1: Add namespace name and scope qualifier

int main()
{
    
    
	printf("%d\n", N::a);//加命名空间的名称及作用域限定符::
	return 0;
}

insert image description here

Supplementary basic concepts:
Scope : the program scope segment that starts to take effect and expires
Life cycle : refers to the period of time during which the object exists during program execution.
Comparison of the two:
scope and life cycle are two completely different concepts.
In English, the scope is represented by "scope" and the life cycle is represented by "duration" .
Scope is a static concept that is only used when compiling source programs. The scope of an identifier refers to the area in the source file where the identifier can legally appear independently.
Life cycle is a runtime (Runtime) concept, which refers to the time period that a variable exists in the process of the entire program from loading to the end of running.
Since functions and data types are static concepts, they do not have a life cycle, and they exist throughout the process from compiling, running the program to ending.

Method 2: Use using to introduce members of the namespace

using N::b;
int main()
{
    
    
	printf("%d\n", N::a);//加命名空间的名称及作用域限定符::
	printf("%d\n", b);//使用using引入命名空间的成员
	return 0;
}

insert image description here

Method 3: Use the using namespace namespace name to import, and expand all the names

using namespace N;//使用using namespace 命名空间名称引入,将所有名称全展开
int main()
{
    
    
	printf("%d\n", a);
	printf("%d\n", b);
	return 0;
}

insert image description here

Advantages of this method: fully expanded, easy to use
Disadvantage: All contents in the library are expanded. If the same name as the name in the library is defined, it will conflict (that is, it is easy to cause naming pollution)

Note: If we usually learn C++ and do exercises, we don't need to think too much about this disadvantage, just use it directly. Only when we are really involved in large C++ development projects, we need to care about this issue.

#include<iostream>
//using namespace std;//C++库中的所有东西都是放到std命名空间中
using  std::cout;
using  std::endl;
int main()
{
    
    
	cout << "hello world!" << endl;
	//std::cout << "hello world!"<< std::endl;
	int i = 1;
	double d = 1.111;
	cout << i << endl;
	cout << d << endl;
	return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/QIYICat/article/details/119739456