17.2 namespace
Namespace provides a more controllable mechanism to prevent name conflicts. namespaces can be divided into global namespaces, making it easier to use independently developed libraries. A namespace is a scope. You can define the name, author, and user of the database in the namespace to avoid the inherent limitations of global names.
17.2.1 namespace Definition
The namespace definition starts with the keyword namespace, followed by the namespace name.
Namespace Anders
{
Namespace NameSpace2
{
Class Class1 {
};
Class Class2 {
};
}
}
Namespace Anders
{
Namespace NameSpace1
{
Class Class1 {
};
Class Class2 {
};
}
}
Namespace Anders
{
Namespace NameSpace2
{
Class Class1 {
};
Class Class2 {
};
}
}
Namespace Anders
{
Namespace NameSpace1
{
Class Class1 {
};
Class Class2 {
};
}
} Using namespace Anders: NameSpace1;
Using namespace Anders: NameSpace2;
Using namespace Anders: NameSpace1;
Using namespace Anders: NameSpace2; like other names, the namespace name must be unique in the scope that defines the namespace. A namespace can be defined within a global scope or other scope, but not within a function or class.
The namespace name is followed by a piece of description and definition enclosed by curly braces. You can put any declaration that can appear in the global scope in the namespace: Class, variable (and their initialization), functions (and their definitions), templates, and other namespaces.
The namespace scope cannot end with a semicolon.
1. Each namespace is a scope
An object defined in a namespace is called a namespace member. As with any scope, each name in the namespace must reference the unique entity in the namespace. Because different namespaces introduce different scopes, different namespaces can have members with the same name.
The name defined in the namespace can be directly accessed by other Members in the namespace. The code outside the namespace must indicate the namespace in which the name is defined.
2. Use namespace members outside the namespace
Using Anders: NameSpace1: Class1;
Using Anders: NameSpace1: Class1; 3. The namespace can be discontinuous.
Unlike other scopes, namespaces can be defined in several sections. A namespace consists of the sum of its separated definition parts. A namespace is cumulative. The separation part of a namespace can be dispersed in multiple files, and namespace definitions in different text files are also cumulative. Of course, the name is only visible in the file where the name is declared. This general restriction continues to be applied. Therefore, if one part of the namespace needs to be defined in another file, the name must still be declared.
// Namespace1.h
# Ifndef NAMESPACE1_H
# Define NAMESPACE1_H
Namespace andsoft
{
Namespace Namespace1
{
Class Class1 {};
}
}
# Endif
// Namespace1.h
# Ifndef NAMESPACE1_H
# Define NAMESPACE1_H
Namespace andsoft
{
Namespace Namespace1
{
Class Class1 {};
}
}
# Endif // Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
}
# Endif
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
}
# Endif4. separation of interfaces and Implementations
The namespace definition can be discontinuous, which means that the separated interface file and the implementation file can be used to form a namespace. Therefore, you can use the same method as managing your own class or function definitions to organize the namespace:
(1) Defining the namespace members of a class and the function declaration or object Declaration as part of the class interface can be placed in the header file, and the file using the namespace member can contain these header files.
(2) namespace member definitions can be placed in separate source files.
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
}
}
# Endif
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
}
}
# Endif // Namespace2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
}
// Namespace2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
} In this way, the namespace can be organized to meet the requirements that different entities (non-inline functions, static data members, variables, etc.) can be defined only once in one program, this requirement also applies to the names defined in the namespace. By separating interfaces and implementations, you can ensure that the function and other names we need are defined only once, but the same declaration can be seen anywhere where the object is used.
To define multiple irrelevant types of namespaces, separate files should be used to indicate each type defined in the namespace.
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
}
# Endif
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
}
# Endif // Class1.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
}
// Class1.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Namespace andsoft
{
Namespace Namespace2
{
Class Class1 {};
}
} // Class2.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Namespace andsoft
{
Namespace Namespace2
{
Class Class2 {};
}
}
From xufei96's column