Introduction and use of C++ namespace namespace

  • 2020-06-12 10:05:49
  • OfStack

introduce

Namespaces can be used to resolve namespaced conflicts in programs, especially in large multi-person projects. For example, we use the std namespace with the standard output of C++ std::cout.

Grammar:


namespace XXX
{
 class A
 {
 public:
 ...
 };
} // No semicolon 

There is namespace in some third libraries, so when we use these libraries, we want to make sure that we use using namespace to unprefix the name.

use


#include <stdio.h>
namespace XXX
{
 class A
 {
 public:
 void test()
 {
  printf("this is namespace XXX class A\n");
 }
 };
}
using namespace XXX;
int main()
{
 A* p = new A();
 p->test();
 return 1;
}

Namespaces also support nesting

conclusion


Related articles: