Namespacing

 

What is it?

A method to avoid naming conflicts of variables, functions and user defined data types by grouping them in logical named categories.

Real world example…  John Smith… 

The name John Smith is not unique with regard to all named people.  To identify which John Smith is intended you must be more specific.  For example, Captain John Smith.  Now we have uniquely identified which John Smith, assuming there is only one John Smith defined within the namespace Captain.

 Namespace Declaration (Definition from MSDN Library)

Identifies and assigns a name to a declarative region.

namespace [identifier] { namespace-body }

The identifier in a namespace declaration must be unique in the declarative region in which it is used. The identifier is the name of the namespace and is used to reference its members.

The declarative region of a namespace declaration is its namespace-body.

Example:

namespace A

{

       int x;

}

 

namespace B

{

       char x;

}

 

void main()

{

       //x = 3; //error C2065: 'x' : undeclared identifier

       //x = 'a'; //error C2065: 'x' : undeclared identifier

      

       A::x = 3; // good use

       B::x = 'a'; // good use

}

 

 

 

STD  & “using”

The entire C++ standard library now lives in the namespace std.

using Directive

 

using namespace namespace-name;

using namespace std;

 

The using-directive allows all of the names in a namespace to be used without the namespace-name as an explicit qualifier.

 

using Declaration

 

using  unqualified-id

using  std::cout

 

The using declaration introduces a name into the declarative region in which the using declaration appears. The name becomes a synonym for a variable or function declared elsewhere. It allows an individual name from a specific namespace to be used without explicit qualification.

 

Examples:

 

In the past

All code in this first example should be familiar to you.   We have included iostream.h in this cpp file in order to use the cout and endl functionality.  The problem is that by doing so we pollute the global name space.  We have included much more than what we needed or had intended.  This style of importing functionality is deprecated, is planed to be fazed out.

#include<iostream.h>

 

void main()

{

       cout << "Old method, this will work for a while but it is deprecated" << endl;

}

 

Wow!

In our first example of using namespacing you should first take note that in the include statement “iostream” is missing its “.h”.  That indicates to the compiler that the namespaces within iostream.h are to be made available, but not all of the functionality directly.   This example shows that the functionality within iostream.h can be use by specifying the namespace, “std”, then using the scope resolution operator, “::”, then the actual functionality.

#include<iostream>

 

void main()

{

       std::cout << "Example specifying everything" << std::endl;

}

 

using directive

When the “using” directive all of the names in a namespace to be used without the namespace-name as an explicit qualifier.  This is known as overkill.  It is using a nuclear bomb to destroy an anthill.  Using this method is convenient but it results in the same problem we were trying to solve, pollution of the global namespace.

 #include<iostream>

 

using namespace std;

 

void main()

{

       cout << "Example with \"using directive\"" << endl;

       cout << "This is using a nuclear bomb to destroy an anthill" << endl;

 

}

 

using declarations

This is the way to do it!  The using expression indicates exactly what functionality will be used from the std namespace.  All other functionality in that namespace is unknown within this scope.  This results in our code looking a little cleaner, avoiding the Wow, a clean global namespace, and self documenting code. 

#include<iostream>

 

using std::cout;

using std::endl;

 

void main()

{

       cout << "Example \"using declarations\"" << endl;

}