
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- What are nested namespaces in C#?
- What are namespaces in C#?
- How to define namespaces in C#?
- How to use namespaces in C++?
- How will you compare namespaces in Python and C++?
- Nested Classes in C#
- Nested Classes in C++
- Nested Tuples in C#
- Nested functions in C
- Can we create nested TitiledBorder in Java?
- Namespaces and Scoping in Python
- Namespaces and Scope in Python
- PHP Namespaces Overview
- PHP Using namespaces
- How can we use nested transactions in MySQL?

Advertisements