C++ using Keyword
Example
Use a variable from a namespace without writing the namespace name:
namespace MyNamespace {
int x = 42;
}
using namespace MyNamespace;
int main() {
cout << x; // No need to write MyNamespace::x
return 0;
}
Definition and Usage
The using keyword lets you use the names in a namespace without the namespace's prefix. With using namespace MyNamespace;, you can write x instead of MyNamespace::x.
This is why most C++ programs start with using namespace std;. Then you can write cout instead of std::cout.
In C++11 and later, using can also give a data type a new name, like using Number = int;. This works like typedef.
Related Pages
Read more about namespaces in our C++ Namespaces Tutorial.