SoFunction
Updated on 2025-03-03

Detailed explanation of the concept and usage of namespace in C++

What is a namespace?

To understand literally, namespaces have two main points - "name" and "space".

Through these two keywords, I will briefly describe to you what a namespace is (see below ↓)

For example:

There is a school where the world is so big that it is strange. Then you may ask, what is strange about this school? What’s strange about this school is that not only are there many students with the same birthday, but also many students share names and surnames. Then one day after school, someone in the school picked up a wallet with Zhang San's student ID in it, and the wallet was sent to the principal. Seeing that the student was so sensible, the principal praised him very much, so he turned on the radio and praised the student to each class and asked the student named Zhang San to come to the principal's office. A few minutes later, the principal opened the door and saw seven or eight big men standing outside the door. It turned out that these were Zhang San from each class 2, 3 and 4, and Zhang San from each grade. The principal was instantly confused and didn't know which Zhang San he was calling.

The story is over. Each class and grade above has different "spaces", Zhang San is a "name", and the confused principal is naturally the compiler. The "principal" is confused and the program will naturally not be executed normally. So why didn’t the principal find the real Zhang San? The reason is that variables, functions, and class naming in each space conflict and naming is polluted by each other. Therefore, in order to solve this problem, we added the namespace. After that, the principal wanted to find Zhang San again, and he would call Zhang San from Class 2, 3rd Class of Senior High School to come to my office on the radio, and he could accurately find what he needed to find.

concept

In C/C++, variables, functions and classes to be learned later exist in a large number of them. The names of these variables, functions and classes will all exist in the global scope, which may cause many conflicts.

The purpose of using namespaces is to localize the names of identifiers to avoid naming conflicts or name pollution, and the namespace keyword is targeted at this problem.

definition

namespace + namespace name + { namespace member }

Features

1. Ordinary namespace

namespace N1 // N1 is the name of the namespace{
    // The content in the namespace can define both variables and functions    int a;
    int Add(int left, int right)
    {
        return left + right;
    }
}

2. Namespaces can be nested

namespace N2
{
    int a;
    int b;
    int Add(int left, int right)
    {
        return left + right;
    }
    namespace N3
    {
        int c;
        int d;
        int Sub(int left, int right)
        {
            return left - right;
        }
    }
}

3. Multiple namespaces with the same name are allowed in the same project (the compiler will synthesize them into the same namespace)

// The compiler will finally synthesize the same namespacenamespace N1
{
    int Mul(int left, int right)
    {
        return left * right;
    }
}

A namespace defines a new scope, and everything in the namespace is limited to

Three ways to use namespaces

1. Add namespace name and scope qualifier

int main()
{
    printf("%d\n", N::a);
    return 0;
}

2. Use using to introduce members in the namespace

using N::b;
int main()
{
    printf("%d\n", N::a);
    printf("%d\n", b);
    return 0;
}

3. Use the namespace name to introduce

using namespce N;
int main()
{
    printf("%d\n", N::a);
    printf("%d\n", b);
    Add(10, 20);
    return 0;
}

This is the article about the concept and usage of namespaces in C++. For more relevant namespace content in C++, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!