Can namespaces be nested in C++?

1.7

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.

#include <iostream>

int x = 20;
namespace outer {
  int x = 10;         
  namespace inner {
    int z = x; // this x refers to outer::x
  }
}

int main()
{
  std::cout<<outer::inner::z; //prints 10
  getchar();
  return 0;
}

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.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



1.7 Average Difficulty : 1.7/5.0
Based on 21 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.