EZ

Eduzan

Learning Hub

Eduzan
Eduzan / C++

Namespaces

A namespace in C++ provides a context where you can declare identifiers such as variables, methods, and classes. It helps in organizing code and avoiding name collisions. For instance, if your code has a function named xyz() and a library also has a function with the same name, the compiler will not be able to distinguish between the two without namespaces. A namespace helps resolve this issue by adding context to the function, class, or variable name.

A namespace essentially defines a scope, and one of its major benefits is preventing name collisions. A well-known example is the std namespace in the C++ Standard Library, where various classes, methods, and templates are defined. While coding in C++, we commonly use using namespace std; to access these elements without needing to prefix std:: to each function call or variable access.

Defining a Namespace

To declare a namespace, use the namespace keyword followed by the namespace name:

namespace my_namespace {
    int variable;
    void function();
    class MyClass {};
}

No semicolon is needed after the closing brace of a namespace definition. To access elements within the namespace, use the following syntax:

my_namespace::variable;
my_namespace::function();

Using the using Directive

The using directive allows you to avoid specifying the namespace every time you use a variable or function. By writing using namespace my_namespace;, you tell the compiler that all identifiers in the code are from the given namespace.

#include <iostream>
using namespace std;

namespace first_space {
    void func() {
        cout << "Inside first_space" << endl;
    }
}

namespace second_space {
    void func() {
        cout << "Inside second_space" << endl;
    }
}

using namespace first_space;

int main() {
    func();  // This calls the function from first_space
    return 0;
}

Output:

Inside first_space

The using directive applies from the point where it’s used until the end of the scope. If another entity with the same name exists in a broader scope, it gets hidden.

Nested Namespaces

Namespaces can also be nested, where one namespace is defined within another. For example:

namespace outer {
    namespace inner {
        void func() {
            std::cout << "Inside inner namespace" << std::endl;
        }
    }
}

Output:

Inside second_space

Scope of Entities in a Namespace

Entities, such as variables and functions, defined in a namespace are scoped within that namespace. This means you can have entities with the same name in different namespaces without causing errors.

#include <iostream>
using namespace std;

namespace first_space {
    void func() {
        cout << "Inside first_space" << endl;
    }
}

namespace second_space {
    void func() {
        cout << "Inside second_space" << endl;
    }
}

int main() {
    first_space::func();  // Calls function from first_space
    second_space::func();  // Calls function from second_space
    return 0;
}

Output:

Inside first_space
Inside second_space

Namespaces in Practice

Consider the following code that demonstrates the error caused by using two variables with the same name in the same scope:

int main() {
    int value;
    value = 0;
    double value;  // Error: redeclaration of 'value'
    value = 0.0;
}

Error:

Compiler Error: 'value' has a previous declaration as 'int value'

With namespaces, you can declare two variables with the same name in different namespaces without conflict:

#include <iostream>
using namespace std;

namespace first_space {
    int val = 500;
}

int val = 100;

int main() {
    int val = 200;
    cout << first_space::val << endl;
    return 0;
}

Output:

ns::Geek::display()

Alternatively, a class can be declared inside a namespace and defined outside the namespace:

#include <iostream>
using namespace std;

namespace ns {
    class Geek;
}

class ns::Geek {
public:
    void display() {
        cout << "ns::Geek::display()" << endl;
    }
};

int main() {
    ns::Geek obj;
    obj.display();
    return 0;
}

Output:

ns::Geek::display()
End of lesson.