17.2.5 class, namespace, and scope
1. Search for real parameters and class-type parameters
Functions (including overload operators) in the namespace defined in the same namespace as the class parameter that accepts the class type parameter (or class type pointer to reference the parameter ), it is visible when a Class Object (or class type reference and pointer) is used as a real parameter.
If a function has a class-type parameter, the function is visible because non-member functions that are conceptually part of the class interface can be used without a separate using declaration. The ability to use non-member operations is particularly useful for operator functions.
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
Namespace andsoft
{
Class Class4 {};
Void Method4 (Class4 & c1 ){}
}
# Endif
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
Namespace andsoft
{
Class Class4 {};
Void Method4 (Class4 & c1 ){}
}
# Endifandresoft: Class4 c4 = randsoft: Class4 ();
Method4 (c4 );
Andresoft: Class4 c4 = andresoft: Class4 ();
Method4 (c4); 2. Implicit AIA and namespaces
When a class is declared as a friend function, the function declaration does not need to be visible. If no visible Declaration exists, you can put the declaration of this function or class into the peripheral scope. If the class is defined within the namespace, no other declared Enis are declared in the same namespace.
Because this element accepts the real parameters of the class type and is implicitly declared in the same namespace as the class, you do not need to use an explicit namespace qualifier when using it.
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
Namespace andsoft
{
Namespace Namespace6
{
Class Class5 {};
Class Class4 {
Friend class Class5;
Friend void Method4 (Class4 * c4 );
};
}
}
Void randsoft: Namespace6: Method4 (randsoft: Namespace6: Class4 * c4 ){}
# Endif
// Namespace3.h
# Ifndef NAMESPACE3_H
# Define NAMESPACE3_H
Namespace andsoft
{
Namespace Namespace6
{
Class Class5 {};
Class Class4 {
Friend class Class5;
Friend void Method4 (Class4 * c4 );
};
}
}
Void randsoft: Namespace6: Method4 (randsoft: Namespace6: Class4 * c4 ){}
# Endifandresoft: Namespace6: Class4 * c4 = new randsoft: Namespace6: Class4 ();
Randsoft: Namespace6: Method4 (c4 );
Method4 (c4 );
Randsoft: Namespace6: Class4 * c4 = new randsoft: Namespace6: Class4 ();
Randsoft: Namespace6: Method4 (c4 );
Method4 (c4 );
From xufei96's column