In C++, a namespace is a way to group related identifiers, such as classes, functions, and variables, under a common name prefix to avoid naming conflicts.
To define a namespace, use the namespace
keyword followed by the namespace name and the set of identifiers to include within the namespace:
namespace my_namespace { // declarations and definitions here }
To use a namespace, either qualify the name of the identifier with the namespace name or bring it into scope with a using
declaration:
my_namespace::my_function(); // using the qualified name using my_namespace::my_function; // using a using declaration my_function(); // using the unqualified name
Namespaces are important for avoiding naming conflicts between different parts of your code, as well as for organizing your code into logical modules.