Understanding Namespace in C++With Examples

A Namespace in C++, informally, is a named scope that we can use to organize our code logically.

This will ensure that variables, functions, and classes with similar functionality are in the same scope. Not only that; it can also help avoid naming collisions since they are within a named scope.

Let’s look at how we can use namespaces in C++ to make our life easier.


What can a Namespace in C++ contain?

A namespace is just a declarative code block, which is bounded using a scope name (called the namespace name).

Since it is a code block, it can contain variables, constants, functions, and classes. But this is a declarative code block, so you can use this only to define all your variables and classes.

Let’s take an example. We’ll construct a namespace called MyNamespace. We’ll leave it empty for now, and add stuff to it later.

This is the syntax for declaring a namespace called MyNamespace. We can insert all of our variables, etc, within this block.

So right now, our namespace is empty. If we want to make this useful for us, let’s add some members to it!

How can we access members of a namespace in C++?

I’ll declare a variable called my_data, of type int, inside this namespace.

Whenever we declare something inside our namespace, we intend to use this from our main program later.

What happens if we try to modify my_data by trying to access it directly?

Output

The compiler complains that it hasn’t seen a variable called my_data in the global scope. How do we deal with this?

The answer lies in the hint that the compiler gives us!

We need to use the scope resolution operator (::) so that our compiler can identify the scope.

Also, this brings me to the topic of a named scope. Since our namespace has a name, the corresponding scope name will be the namespace name!

So, we combine it with the scope resolution operator to get MyNamespace::my_data! That’s it! Now, we can do all the things we desire, since we now have access to this variable.

Let’s rewrite our program to use the correct scope.

Output

Now, we’ve fixed our problem! Let’s now go to another example to show another use case of namespaces – avoiding name collision.

Avoiding name collision between functions

Since we mentioned that the scope of all members inside a namespace is defined by the namespace name, we can use this to avoid renaming functions.

For example, in our namespace, assume that we define a function called add(x, y) to add two numbers.

In our global scope, we can still have another separate function called add(x, y), or even add(x, y, z)! This is because the scopes are different. We don’t need to unnecessarily rename functions now!

I am using modern C++ practices in this example. Some of them are based on the Standard Template Library (STL). I am also using trailing return types, where I can specify the return type of a function after I declare its prototype.

Output

As you can see, we can indeed write two different functions add() on the different scopes.

Let’s now go to another example of namespaces, using Classes.


Using Classes inside Namespaces

We can also use classes in our namespace. The same declaration pattern follows.

Let’s write our driver program to utilize the new namespace members!

Output

We can also rewrite our example, by defining our Class outside the namespace, but using our scope resolution operator.


Accessing members of a namespace in C++ directly

If the namespace dependency becomes long, it may be tedious and time consuming to type out the full scope of the member to access it. To overcome this, C++ introduced the using directive.

This will allow us to access all the names (members) in a namespace directly!

We generally place this directive at the top of a file, so that it applies everywhere in the file.

To use this, the syntax is as follows:

Where namespace_name is your namespace name. In my case, it is MyNamespace. So, this statement will become:

Now, we can directly access members inside of MyNamespace! But keep in mind that if there is a global variable of the same name, the compiler will throw an error.

Output


Conclusion

In this article, we learned about the concept of a namespace in C++, and how we can use them to logically organize our code. We also saw how we can use it to avoid name collisions, and also examples showing the using directive.


References


By admin

Leave a Reply

%d bloggers like this: