Detailed namespace of C++ learning

1. Understand namespaces

The namespace namespace divides the naming space as translated, and each function name or variable has its own scope of use, avoiding the problem of using function name conflicts;

For example, if there are two people named Zhang San in a class, then how to distinguish these two people will have to give them aliases, such as fat Zhang San and thin Zhang San.

 The same is true in the code

2. Using Namespaces

namespace(关键字)  (自定义名字){
   // 代码声明
}
//调用命名空间
(自定义名字)::(变量或者函数);
#include <stdio.h>
namespace std
{

    void cout()
    {
        printf("cout");
    }

}

int main()
{
    std::cout();
    return 0;
}

Some people may think that it is too cumbersome to bring the space name every time the function of the scope is called

You can use the using keyword to omit the space name

#include <stdio.h>
namespace std
{

    void cout()
    {
        printf("cout");
    }

}
using std::cout;
int main()
{
    cout();
    return 0;
}

3. Disjoint namespace

Suppose we are developing a graphics processing library, which contains various graphics-related functions and classes. We can use namespaces to organize and categorize these functions.

First, define a namespace in a file Geometryto contain graphics-related functions and classes:

// Geometry.h

namespace Geometry
{
    class Point
    {
        //...
    };

    void drawLine()
    {
        //...
        printf("drawLine");
    }

    //...
}

We can then extend that namespace in another file, adding more functions and classes:

// Shapes.h

namespace Geometry
{
    class Circle
    {
        //...
    };

    void drawRectangle()
    {
        //...
        printf("drawRectangle");
    }

    //...
}

By using discontinuous namespaces, we can divide the functions logically, such as the Geometry .h namespace is responsible for the operation of basic geometric figures, and Shapes.hthe namespace is responsible for drawing specific shapes, etc.

When using, we can selectively include the required namespaces as needed and use the functions it provides.

// main.cpp
#include <stdio.h>
#include "Geometry.h"
#include "Shapes.h"

int main() {
  
     Geometry::drawLine();
    Geometry::drawRectangle();
    return 0;
}

Through such a design, we can better organize and manage the code, avoid naming conflicts, and when we need to extend the function, we only need to add the corresponding file and expand the namespace.

4. Nested Namespaces

As the name implies, it can be called nestedly like a function

命名空间可以嵌套,您可以在一个命名空间中定义另一个命名空间,如下所示:
namespace namespace_name1 {
   // 代码声明
   namespace namespace_name2 {
      // 代码声明
   }
}
可以通过使用 :: 运算符来访问嵌套的命名空间中的成员:

// 访问 namespace_name2 中的成员
using namespace namespace_name1::namespace_name2;
 
// 访问 namespace_name1 中的成员
using namespace namespace_name1;

for example:

#include <stdio.h>
namespace std
{

    void cout()
    {
        printf("cout");
    }
    namespace std1
    {

        void cout()
        {

            printf("cout1");
        }

    }

}
using std::std1::cout;
int main()
{
    cout();
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_59054762/article/details/131314262