Namespace in C++
- A namespace allows us to group related functions, classes, variables, and other declarations under a specific name.
- It is used to organize code and avoid name conflicts, especially in large projects or when integrating multiple libraries.
Defining a Namespace
- Define a namespace using the
namespacekeyword. - The function add and class MyClass are part of MyNamespace.
Using a Namespace
1) Fully Qualify Names: Use the namespace name as a prefix:
2) using Keyword: Use the using keyword to avoid prefixing each identifier with the namespace:using namespace MyNamespace;
int result = add(3, 4); // No need for MyNamespace::
MyClass obj;
obj.display();
3) Selective Use: We can bring specific members of a namespace into scope:
Anonymous Namespaces
- An anonymous namespace is a namespace without a name.
- It is used to limit the visibility of its contents to the file in which it is declared.
- The function
secretis only accessible within the file where it's defined. - This is same as making the function static.