In C++, namespaces can be nested, and resolution of namespace variables is hierarchical. For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace. In the line “int z = x”, x refers to outer::x. If x would not have been in outer then this x would have referred to x in global namespace.
Output of the above program is 10.
On a side node, unlike C++ namespaces, Java packages are not hierarchical.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Inline namespaces and usage of the "using" directive inside namespaces
- Nested list in C++ STL
- Nested Classes in C++
- Nested switch statement in C++
- Decision Making in C / C++ (if , if..else, Nested if, if-else-if )
- C++ | Nested Ternary Operator
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler