Namespace is a container for identifiers. It puts the names of its members in a distinct space so that they don't conflict with the names in other namespaces or global namespace.
Creating a namespace is similar to creation of a class.
namespace MySpace
{
// declarations
}
int main()
{
// main function
}
This will create a new namespace called MySpace, inside which we can put our member declarations.
Example for Alias:
namespace StudyTonightDotCom
{
void study();
class Learn
{
// class defintion
};
}
// St is now alias for StudyTonightDotCom
namespace St = StudyTonightDotCom;
Example for Unnamed namespace:
namespace
{
class Head
{
// class defintion
};
// another class
class Tail
{
// class defintion
};
int i,j,k;
}
int main()
{
// main function
}
For example, below is some header1.h header file, where we define a namespace:
namespace MySpace
{
int x;
void f();
}
We can then include the header1.h header file in some other header2.h header file and add more to an existing namespace:
#include "header1.h";
namespace MySpace
{
int y;
void g();
}
There are three ways to use a namespace in program,
::
)using
directiveusing
declaration::
)Any name (identifier) declared in a namespace can be explicitly specified using the namespace's name and the scope resolution ::
operator with the identifier.
namespace MySpace
{
class A
{
static int i;
public:
void f();
};
// class name declaration
class B;
//gobal function declaration
void func();
}
// Initializing static class variable
int MySpace::A::i=9;
class MySpace::B
{
int x;
public:
int getdata()
{
cout << x;
}
// Constructor declaration
B();
}
// Constructor definition
MySpace::B::B()
{
x=0;
}
using
directiveusing
keyword allows you to import an entire namespace into your program with a global scope. It can be used to import a namespace into another namespace or any program.
Conside a header file Namespace1.h:
namespace X
{
int x;
class Check
{
int i;
};
}
Including the above namespace header file in Namespace2.h file:
include "Namespace1.h";
namespace Y
{
using namespace X;
Check obj;
int y;
}
We imported the namespace X
into namespace Y
, hence class Check
will now be available in the namespace Y
.
Hence we can write the following program in a separate file, let's say program1.cpp
#include "Namespace2.h";
void test()
{
using Namespace Y;
// creating object of class Check
Check obj2;
}
Hence, the using
directive makes it a lot easier to use namespace, wherever you want.
using
declarationWhen we use using
directive, we import all the names in the namespace and they are available throughout the program, that is they have a global scope.
But with using
declaration, we import one specific name at a time which is available only inside the current scope.
NOTE: The name imported with using
declaration can override the name imported with using
directive
Consider a file Namespace.h:
namespace X
{
void f()
{
cout << "f of X namespace\n";
}
void g()
{
cout << "g of X namespace\n";
}
}
namespace Y
{
void f()
{
cout << "f of Y namespace\n";
}
void g()
{
cout << "g of Y namespace\n";
}
}
Now let's create a new program file with name program2.cpp with below code:
#include "Namespace.h";
void h()
{
using namespace X; // using directive
using Y::f; // using declaration
f(); // calls f() of Y namespace
X::f(); // class f() of X namespace
}
f of Y namespace f of X namespace
In using declaration, we never mention the argument list of a function while importing it, hence if a namespace has overloaded function, it will lead to ambiguity.