Namespaces in C++

In this tutorial, we will learn about Namespaces in C++.

Namespaces in C++

Let’s start with understanding what is namespace and why do we need it. A namespace is a declarative region that provides a scope to the identifiers inside it. The identifier here can be both a function name or a variable etc. Sometimes, the user defines a function or a variable whose name collides with some already existing function or variable name in some other library. Due to this, there is a conflict for the compiler regarding which one to refer to. Thus, by using namespace we can define in which context the names are declared/defined. Hence, namespace defines the scope for its identifiers.

Defining Namespace

The following is the syntax for defining a namespace :

namespace namespace_name
 {
   // code declaration
 }

Example :

namespace one
{
  int a = 2, b = 3;
  void func()
  {
    cout << "namespace one\n"
      "a = " << a << "\n b = " << b;
  }
}

namespace two
{
  int a = 22, b = 33;
  class name
  {
  public :
    void func1()
    {
      cout << "namespace two, class name\n"
        " a = " << a << "\n b = " << b;
    }
  };
  void func2()
  {	
    cout << "naespace two, func2\n"
      " a = " << a << "\n b = " << b;
  }
}

The above example shows how to make a new namespace. The namespace one contains two variables and a function. However, the second namespace two contains a class and a function.

Accessing namespace members in C++

Consider the above example and the two namespaces defined. We have three ways of accessing the members of a namespace.

  1. To use a fully qualified name everytime we access a member :
two::name n;
n.func1();
two::func2();

2. To use a using declaration to bring one identifier into scope:

using two::name;
name n;
n.func1();

3. To use a using directive to bring everything in the namespace into scope:

using namespace two;
name n;
n.func1();
func2();

 Namespaces aliases

Since we use namespaces to remove any ambiguity in the code, the name of a namespace should be unique. This means that sometimes the names of namespaces can be very long. Due to this, accessing members becomes tedious and difficult. Hence, we can have an alias name for that particular long namespace and use that alias instead of the original name. We define the namespace alias as follows :

namespace extremely_long_namespace_name
{ void func(); }
namespace NAME = extremely_long_namespace_name;
NAME::func();

Anonymous or unnamed namespace

C++ also allows us to have unnamed or anonymous namespaces. This means that they do not need a name to operate. This makes the code invisible to other files. Hence, the namespace is hidden from other files. This can be defined as follows :

namespace
{
    void Func(){}
}

Program to show Namespaces in C++

#include <iostream>
using namespace std;

namespace one
{
  int a = 2, b = 3;
  void func()
  {
    cout << "namespace one\n"
      " a = " << a << "\n b = " << b << endl;
  }
}

namespace two
{
  int a = 22, b = 33;
  class name
  {
  public :
    void func1()
    {
      cout << "namespace two, class name\n"
        " a = " << a << "\n b = " << b << endl;
    }
  };
  void func2()
  {
    a++;
    b++;
    cout << "namespace two, func2\n"
      " a = " << a << "\n b = " << b << endl;
  }
}
int main()
{
  one::func();          //accessing func from namespace one 

  using namespace two;    //for namespace two
  name n;       
  n.func1();     //accessing func1
  func2();       //accessing func2

  return 0;
}

Output :

namespace one
a = 2
b = 3
namespace two, class name
a = 22
b = 33
namespace two, func2
a = 23
b = 34

Hope this was helpful. Enjoy Coding!

Also learn :

Data Type Modifiers in C++

Signal Handling in C++

Leave a Reply

Your email address will not be published. Required fields are marked *