What is a namespace in C++? Provide examples as well.
Before we discuss exactly what a namespace is, it is probably best to consider a simple example of when and why we would need namespaces. Take a look at these two fictional C++ header files:
// someheaderfile.h class Example { ... }; // somelibfile.h class Example { ... };
Note that it would be impossible to use both of these header files in a single program, because there are two classes with the exact same name – “Example”. Obviously, if we try to use the “Example” class after including both of the header files, it would not be clear which Example class we would be referring to – the one in the someheaderfile.h file or the one in the somelibfile.h file.
So, this is where namespaces can help. Think of a namespace as a declarative “region” that forces the attachment of an extra identifier to any names declared inside that region. That’s the formal definition, which may be confusing. Take a look at an example below to help clarify.
Just remember that if the “Example” classes that we showed above are in separate namespaces, then each name will be unique because of the addition of the namespace identifier. Here’s an example of namespaces in action.
C++ namespace example
// someheaderfile.h namespace SomeHeader { class Example { ... }; } // somelibfile.h namespace SomeLib { class Example { ... }; }
Now that we have namespaces, we can call each of the “Example” classes without having there be any confusion as to which “Example” class we are calling – the one in the SomeLib file or the one in the SomeHeader file. In other words, the class names won’t clash. But, how do we actually make those calls using the correct syntax?
Well, we can use the scope resolution operator – the “::” that you have probably seen before in C++. So, to access each of those classes using their respective namespace, it would look like this:
SomeHeader::Example SomeLib::Example
So, as you can see in the code above, using the namespace essentially allows us to be more “specific” with regards to which Example class we want to reference. Now there is no ambiguity, and no name clashing.
Namespaces and using keyword example
We could also employ the using keyword to put a namespace into action. Let’s go through a quick example of what that would look like. Suppose we have the following namespace called SomeNameSpace:
namespace SomeNameSpace { class someClass { static int j; public: static void someFunc(); }; }
Let’s suppose that we want to set the variable “j” declared above by using the scope resolution operator. This is what that would look like:
int SomeNameSpace::someClass::j=5;
But, what if we ant to call the function someFunc() that we declared in class X above? How could we do that with the using keyword? Well, here’s what that would look like:
class Z { using namespace SomeNameSpace; someClass::someFunc(); };
The way the using keyword works in the example above is that it essentially says that any code coming after the “using namespace SomeNameSpace;” line will “use” the SomeNameSpace namespace. This means that we can reference someFunc with just a call to someClass. The important thing to note here is that we did not have to make the call to someFunc by explicitly referencing the namespace – because the namespace was declared beforehand with the using keyword. In other words we did not have some code that looked like this:
SomeNameSpace::someClass::someFunc();
So, hopefully it’s clear why and when the using keyword would be of great “use” (no pun intended). It helps save unnecessary and repetitive calls to the namespace.