c++ | namespace namespace


1. Naming conflicts

insert image description here
insert image description here
Because we included the <stdlib.h> header file, which contains the rand function, the compiler does not know whether you want to print the global variable rand or the rand function.
insert image description here
We defined a namespace A, and the compilation passed at this time, and the address of the rand function is output here.

Two, namespace namespace

1. Namespaces can store variables, structures, classes, and functions

namespace B
{
    
    
	int a = 10;
	void test()
	{
    
    
		cout << "hello world" << endl;
	}

	struct S {
    
    };
	class C {
    
    };
}

2. The namespace must be declared in the global scope

insert image description here

We defined a namespace C in the local scope and reported an error.

3. Namespaces can nest namespaces

namespace D
{
    
    
	int a = 10;
	namespace E
	{
    
    
		int a = 10;
	}
}

4. Namespaces with the same name will be merged

insert image description here

4. Namespaces can be anonymous

insert image description here

4. The namespace can be aliased

insert image description here


Guess you like

Origin blog.csdn.net/2301_77412625/article/details/130134093