![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can namespaces be nested in C++?
Yes the namespace can be nested in C++. We can define one namespace inside another name space as follows −
Syntax
namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }
You can access members of nested namespace by using resolution operators as follows −
// to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;
Example
#include <iostream> using namespace std; // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } } using namespace first_space::second_space; int main () { // This calls function from second name space. func(); return 0; }
Output
Inside second_space
Advertisements