Previous: http://www.bkjia.com/kf/201201/115811.html
17.2.3 untitled namespace
A namespace can enable an unnamed, unnamed namespace to be defined without a name. The Untitled namespace starts with the keyword namespace. After the keyword namespace, It is a block that is bounded by curly brackets.
Unlike other namespaces, an unnamed namespace is defined partial to a specific file and never spans multiple text files.
An untitled namespace can be discontinuous in a given file, but cannot span the file. Each file has its own untitled namespace.
An unnamed namespace is used to declare objects that are partial to a file. Variables defined in an unnamed namespace are created at the beginning of the program and exist until the end of the program.
Names defined in Unnamed namespaces can be used directly. After all, there is no namespace name to limit them. You cannot use the scope operator to reference untitled namespace members.
The names defined in an unnamed namespace are only visible to the files that contain the namespace. If another file contains an unnamed namespace, the two namespaces are irrelevant. Two namespaces can define the same name, and these definitions reference different entities.
The names defined in an unnamed namespace can be found in the scope where the namespace is defined. If an unnamed namespace is defined in the outermost scope of the file, the name of an untitled namespace must be different from that defined in the global scope.
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
Namespace
{
Class Class3;
}
}
# Endif
// Namespace2.h
# Ifndef NAMESPACE2_H
# Define NAMESPACE2_H
Namespace andsoft
{
Namespace Namespace2
{
Class Class1;
Class Class2;
}
Namespace
{
Class Class3;
}
}
# Endif // Class3.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Class randsoft: Class3 {
Randsoft: Namespace2: Class2 * c2;
};
// Class3.cpp
# Include "stdafx. h"
# Include "NameSpace2.h"
Class randsoft: Class3 {
Randsoft: Namespace2: Class2 * c2;
}; Before introducing a namespace in Standard C ++, the program must declare the names as static so that they are local to a file. The static declaration in the file is inherited from the C language. In the C language, the local entity declared as static is invisible outside the file that declares it.
C ++ does not approve of static file declaration. A feature that is not supported in future versions is a feature that may not be supported. Do not use namespaces instead of static files.
From xufei96's column