Names in C++ can refer to variables, functions, structures, enumerations, classes, and class and structure members. When programming projects grow large, the potential for name conflicts increases. When you use class libraries from more than one source, you can get name conflicts. For example, two libraries might both define classes named List, Tree, and Node, but in incompatible ways. You might want the List class from one library and the Tree from the other, and each might expect its own version of Node. Such conflicts are termed namespace problems.
The C++ Standard provides namespace facilities to provide greater control over the scope of names. It has taken a while for compilers to incorporate namespaces, but, by now, support has become common.
example:
Code:
namespace Jack {
double pail;
void fetch();
int pal;
struct Well { ... };
}
Placing a using-directive at the global level makes the namespace names available globally. You've seen this in action many a time:
Code:
#include <iostream> // places names in namespace std
using namespace std; // make names available globally
Placing a using-directive in a particular function makes the names available just in that function.
Code:
int vorn(int m)
{
using namespace jack; // make names available in vorn()
...
}