Namespaces can be nested, which means that you can define one namespace inside another called a nested namespace, as shown below:
namespace namespace_name1 {
// code declarations
namespace namespace_name2 {
// code declarations
}
}
The following resolution operators can be used to access members of a nested namespace:
// to access members of namespace_name2
using namespace namespace_name1::namespace_name2;
// to access members of namespace:name1
using namespace namespace_name1;
Now, Let’s see how nested namespace work through an example:
#include <iostream> using namespace std; namespace Outer_nameSpace { void func() { cout << "Outer NameSpace." << endl; } namespace Inner_nameSpace { void func() { cout << "Inner NameSpace." << endl; } } } using namespace Outer_nameSpace::Inner_nameSpace; int main() { //Outer_nameSpace::func(); func(); return 0; }
Output:
Inner NameSpace.
For calling outer namespace then we declare
Outer_nameSpace::func();
It prints the outer namespace function func().
Outer NameSpace.
Join Our Community
Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.
Related Articles: