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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Inline namespaces and usage of the "using" directive inside namespaces
- Namespaces in C++ | Set 4 (Overloading, and Exchange of Data in different Namespaces)
- Nested list in C++ STL
- Nested Classes in C++
- Nested Loops in C++ with Examples
- C++ | Nested Ternary Operator
- Decision Making in C / C++ (if , if..else, Nested if, if-else-if )
- Nested switch statement in C++
- Learn C++ Programming Step by Step - A 20 Day Curriculum!
- Why do we need reference variables if we have pointers
- Similarities and Difference between Java and C++
- Macros and its types in C/C++
- How to find index of a given element in a Vector in C++
- Introduction to Complex Objects and Composition