Sunday, May 27th

Last update12:59:40 PM GMT

Namespaces in C++

Write e-mail

Namespaces in C++

argaiv1287

Namespaces in C++ have gained popularity quite recently. Before we discuss what are namespaces we should first understand why we need namespaces.

Suppose you have two developers working on different modules of a code and each of them builds a different class library. Now at the time of integration you include the headers (header1.h and header2.h) of both the libraries in the mainstream code.

But later to your surprise at compile time you come to know that both the libraries were using a function with the same name and hence errors at compilation.

What to do now?

Obviously changing the names of one of the two classes throughout its source library can solve the problem, but usually this is not what anyone will like to do, not just because of the effort it will require but more so because, there can be a logical reasoning behind having the same class name in both the libraries as they might be serving almost the same purpose, even if they are differing in their member functions and member data.

So what do we do now? Here is when namespaces come to your rescue.

Namespaces are used to structure entities like variables, classes, objects and functions under a common scope.

Namespaces gain importance in real development projects where we frequently have cases of different programmers using the same variable names in different parts of the code and so it becomes essential to find a way out to avoid redefinition errors and still keeping the logic intact. By use of namespaces the global scope can be broken down into various sub-scopes. 

By use of namespaces the global scope can be broken down into various sub-scopes. 

namespace header1
{
int myVar = 10;
}
namespace header2
{
double myVar = 3.1416;
}
int main () {
cout << header1::myVar << endl;
cout << header2::myVar << endl;
return 0;
}
 

Let us now refer to the above code.In this case there are two global variables having the same name myVar, but by forcing them under their respective namespaces and accessing them using scope operator (::), we are able to differentiate between the two myVar. Hence we have no redefinition errors.

However, this example does not depict the real use of namespaces. The most common use of namespaces is by means of the “using” keyword.

Keyword “using”

The keyword using can be used to introduce an entire namespace inside a section of code.

namespace header1
{
 int myVar = 10;
class Entity{...};
}
namespace header2
{
 double myVar = 3.1416;
 class Entity{...};
}
int main ()
{
 using namespace header1;
 Entity e1; 
.... 
 return 0;
 }

In the above snippet after the directive “using namespace header1”, the subsequent names in the current region will refer to the header1 namespace without requiring  the class name e1 to be qualified with the namespace name.

What will happen if we say:

using namespace header1;
using namespace header2;

Will there be a conflict at this point? The answer is "no", unless we actually try to use the common class Entity. The using directive simply makes the names under a namespace available inside the current region. So usage like:

Entity e1;

in the above case would give an error, while:

header1::Entity e1;

header2::Entity e2;

will still work.

Please note here that “using ” and “using namespace ” are valid only in the block in which they are declared (or in the entire code if declared globally), hence if we wish to first use header1 names in our code and then of header2, we should do something like this:

int main () {
{
using namespace header1;
cout <<< endl;
Entity e1;
}
{
using namespace scope2;
cout <<< endl;
Entity e2;
}
return 0;
}

Another way of accessing names is by means of a using declaration:

using header1::Entity;

Entity e1;

Is this a bit confusing? Let’s make it simple.

A using directive:

using namespace header1;

says that all the names in namespace header1  are available for use, but none of them are actually declared. But a using declaration, on the other hand, actually introduces a name into the current scope. That is:

using namespace header1;

makes Entity and myVar available for use, but doesn't declare them. But saying:

using header1::Entity;

actually introduces header1::Entity into the current scope as a declaration.

Saying:

using header1::Entity;

using header2::Entity;

will trigger a "duplicate declaration" compiler error.

Another point worth mentioning here is that we frequently encounter using namespace std; statement in C++ programs. This is simply because all the source files in C++ standard library have their entities defined under namespace std.

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting