HP aC++/HP C A.06.20 Programmer's Guide

// in the outer space
char const* name() const { return Object from M::X; }
};
}
}
int main() {
N::Object o1;
M::Object o2;
M::X::Object o3;
printf(This object is: %s.\n, o1.name());
printf(This object is: %s.\n, o2.name());
printf(This object is: %s.\n, o3.name());
return 0;
}
Connections Across Translation Units
If a type, function, or object is declared inside of a namespace, then using that entity
will require naming this namespace in some explicit or implicit way; even if the use
happens in another translation unit (or source file).
A unique feature of namespaces is that they can be extended. The following example
shows this; as well as the connections between a namespace extending across different
translation units.
The example also illustrates the concept of unnamed namespaces. These namespaces
can only be extended within a translation unit. Unnamed namespaces in different
translation units are unrelated; hence their names effectively have internal linkage. In
fact, the ANSI/ISO C++ International Standard specifies that using static to indicate
internal linkage is deprecated in favor of using namespaces.
#include <stdio.h>
namespace N {
char const* f() { return f(); }
}
namespace { // An unnamed namespace
char const* f(double);
} // Names in unnamed namespaces are visible in their surrounding scope.
// They cannot be qualified since the space has no name.
namespace N { // An extension of the first part of namespace N
char const* f(int); // Leave the implementation to another
} // translation unit.
int main() {
printf(Calling: %s.\n, N::f()); // OK, declared and defined above
printf(Calling: %s.\n, N::f(7)); // OK, declared above (defined elsewhere)
printf(Calling: %s.\n, f(3.0)); // OK, declared above (defined below)
return 0;
}
namespace { // An extension of the unnamed namespace in this translation unit
HP aC++ Keywords 185