What are namespaces in C++ ?

By | January 24, 2015

What are namespaces in C++ ?

Consider a situation, when we have two persons with the same name, Steve, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area if they live in different area or their mother or father name, etc.

Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.

A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.

Defining a Namespace:

A namespace definition begins with the keyword namespace followed by the namespace name as follows:

 

To call the namespace-enabled version of either function or variable, prepend the namespace name as follows:

 

Let us see how namespace scope the entities including variable and functions:

 

If we compile and run above code, this would produce the following result:

 

The using directive:

You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace. The namespace is thus implied for the following code:

 

If we compile and run above code, this would produce the following result:

The using directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows:

The using directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows:

 

Subsequent code can refer to cout without prepending the namespace, but other items in the stdnamespace will still need to be explicit as follows:

 

If we compile and run above code, this would produce the following result:

 

Names introduced in a using directive obey normal scope rules. The name is visible from the point of theusing directive to the end of the scope in which the directive is found. Entities with the same name defined in an outer scope are hidden.

Discontiguous Namespaces:

A namespace can be defined in several parts and so a namespace is made up of the sum of its separately defined parts. The separate parts of a namespace can be spread over multiple files.

So, if one part of the namespace requires a name defined in another file, that name must still be declared. Writing a following namespace definition either defines a new namespace or adds new elements to an existing one:

 

Nested Namespaces:

Namespaces can be nested where you can define one namespace inside another name space as follows:

 

You can access members of nested namespace by using resultion operators as follows:

 

In the above statements if you are using namespace_name1, then it will make elements of namespace_name2 available in the scope as follows:

 

If we compile and run above code, this would produce the following result: