1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
It is not strictly necessary to i(define members of namespaces) inside a
namespace region. But before an entity is defined em(outside) of a namespace
it must have been declared em(inside) its namespace.
To define an entity outside of its namespace its name must be em(fully
qualified) by prefixing the member by its namespaces. The definition may be
provided at the global level or at intermediate levels in the case of nested
namespaces. This allows us to define an entity belonging to namespace tt(A::B)
within the region of namespace tt(A).
Assume the type tt(int INT8[8]) is defined in the tt(CppAnnotations::Virtual)
namespace. Furthermore assume that it is our intent to define a function
tt(squares), inside the namespace nl()
tt(CppAnnotations::Virtual) returning a
pointer to tt(CppAnnotations::Virtual::INT8).
Having defined the prerequisites within the tt(CppAnnotations::)tt(Virtual)
namespace, our function could be defined as follows (cf. chapter ref(MEMORY)
for coverage of the memory allocation operator tt(new[])):
verb( namespace CppAnnotations
{
namespace Virtual
{
void *pointer;
using INT8 = int[8];
INT8 *squares()
{
INT8 *ip = new INT8[1];
for (size_t idx = 0; idx != sizeof(INT8) / sizeof(int); ++idx)
(*ip)[idx] = (idx + 1) * (idx + 1);
return ip;
}
}
})
The function tt(squares) defines an array of one tt(INT8) vector, and
returns its address after initializing the vector by the squares of the first
eight natural numbers.
Now the function tt(squares) can be defined outside of the
tt(CppAnnotations::)tt(Virtual) namespace:
verb( namespace CppAnnotations
{
namespace Virtual
{
void *pointer;
using INT8 = int[8];
INT8 *squares();
}
}
CppAnnotations::Virtual::INT8 *CppAnnotations::Virtual::squares()
{
INT8 *ip = new INT8[1];
for (size_t idx = 0; idx != sizeof(INT8) / sizeof(int); ++idx)
(*ip)[idx] = (idx + 1) * (idx + 1);
return ip;
})
In the above code fragment note the following:
itemization(
itt(squares) is declared inside of the tt(CppAnnotations::Virtual)
namespace.
itt() The definition outside of the namespace region requires us to use
the fully qualified name of the function em(and) of its return type.
itt() em(Inside) the body of the function tt(squares) we are within the
tt(CppAnnotations::)tt(Virtual) namespace, so inside the function fully
qualified names (e.g., for tt(INT8)) are not required any more.
)
Finally, note that the function could also have been defined in the
tt(CppAnnotations) region. In that case the tt(Virtual) namespace would have
been required when defining tt(squares()) and when specifying its return type,
while the internals of the function would remain the same:
verb( namespace CppAnnotations
{
namespace Virtual
{
void *pointer;
using INT8 = int[8];
INT8 *squares();
}
Virtual::INT8 *Virtual::squares()
{
INT8 *ip = new INT8[1];
for (size_t idx = 0; idx != sizeof(INT8) / sizeof(int); ++idx)
(*ip)[idx] = (idx + 1) * (idx + 1);
return ip;
}
})
|