c++ namespace
what is a namespace
Namespace keyword: namespace
A namespace can be understood as an area where there can be functions, variables, classes...
Namespaces are generally used with the scope qualifier "::"
namespace slowstep { int a=1; void Show() { printf("hellow world\n"); } class Animal { public: int m_age; }; } int main() { int a=2; printf("%d",a); printf("%d",slowstep::a); return 0; }
Why have namespaces
The concept of namespace was introduced in c++ to solve the problem of naming conflicts in c language.
Take a scenario: When multiple people collaborate to complete a c/c++ project, it is easy to have the same function name or variable name, causing naming conflicts. Namespaces are introduced in C++. When multiple people collaborate to complete a project, each Functions or variables written by individuals are placed in their own namespace, so that when calling, the name of the namespace can be added in front of the variable or function to avoid the problem of naming conflicts.
namespace details
-
namespace is the supplement of c++ to c language, there is no namespace in c language
-
When accessing or modifying a variable, by default, it first searches for the definition of the variable in the local domain, and then goes to the global domain to search.
int a=10; int main() { int a=0; printf("%d",a);//The a variable can be found in the local domain, and will not be searched in the global domain, the result is 0 return 0; }
-
When a variable, function, or class only exists in a specific namespace, if you want to use it, you must use the name of the namespace plus the scope qualifier to access it. The compiler will not actively access the contents of the namespace
namespace slowstep { float a=10; void Show() { printf("hellow c++"); } } int main() { printf("%f",a);//First go to the local domain to find a, no; then go to the global domain to find a, no. It will not actively go to the slowstep namespace to find it, so a is an undefined identifier Show();//function analogy return 0; }
Correct way to call:
int main() { printf("%f",slowstep::a); slowstep::Show(); return 0; }
-
Two namespace compilers with the same name will automatically merge them into one during the process of compiling the program
namespace slowstep { int left=0; int right=10; } namespace slowstep { void Test() { printf("hellow c++"); } }
The above code will be processed as
namespace slowstep { int left=0; int right=10; void Test() { printf("hellow c++"); } }
-
Namespaces can be nested
namespace slowstep { int a=10; double b=3.14; namespace slowstep_a { int Add(int a,int b) { return a+b; } char ch='f'; } } int main() { printf("%d",slowstep::a); printf("%lf",slowstep::b); printf("%d",slowstep::slowstep_a::Add(2,3)); printf("%c",slowstep::slowstep_a::ch); return 0; }
Use of namespaces
Take hellow world in c++ as an example to illustrate the use of namespaces
There are three ways to use namespaces
-
Add the scope qualifier and specify which namespace to use
int main() { std::cout<<"hellow world"<<std::endl; return 0; }
It is specified here that the output stream object cout in the standard namespace and the newline symbol endl in the standard namespace are used
-
Expand all namespaces to expose them. The effect of this operation is equivalent to directly expanding the content in the namespace to the global domain. This practice is not recommended, but sometimes for convenience, using namespace std is often used.
#include<iostream> using namespace std; int main() { cout<<"hellow world"<<endl; }
-
Expand the namespace section
using std::cout; using std::cin; int main() { int a=0; cin>>a; cout<<a<<std::endl; return 0; }
In this way, the method of partially expanding the namespace can be used without adding the scope qualifier when using the expanded content, but the scope qualifier still needs to be used when using the unexpanded content.