C++ namespace Keyword
Example
Create two namespaces that both have a variable called x:
namespace First {
int x = 5;
}
namespace Second {
int x = 10;
}
int main() {
cout << First::x << "\n";
cout << Second::x;
return 0;
}
Definition and Usage
The namespace keyword creates a namespace. A namespace groups related code, like variables and functions, under a name.
Two namespaces can have members with the same name, like x in the example, without a conflict. Use the name of the namespace and :: to access a member, like First::x.
The standard library uses the namespace std. This is why many programs have the line using namespace std;.
Related Pages
Read more about namespaces in our C++ Namespaces Tutorial.