[C++]Explore-Namespace

Namespaces

namespace namespace_name
{
    //代码声明
}

To call a function in a namespace, you need to add the name of the space in front:

name::code; // code 可以是变量或者函数

using directive

using namespace

As beginners often write in front of the file:

using namespace std;

It means to use the namespace "std", so that you can directly use the things in "std" without adding the name of the namespace in front. (I didn’t even know what this was before...)

using std::cout

You can also just use a certain function like this, just like "import" in Python

Discrete namespace

A namespace can be written in different files, it is composed of several separately defined parts. Therefore, if a component in the namespace needs to request a name defined in another file, the name still needs to be declared.

Nested namespace

Namespaces can be nested

namespace namespace_name1
{
    // code
    namespace namespace_name2
    {
        // code
    }
}

Call members in the inner layer of nesting:

using namespace namespace_name1::namespace_name2;

Guess you like

Origin blog.csdn.net/weixin_44092088/article/details/110749637