Question

How can I create an array of namespaces? And because it seems like a long shot, if this is impossible, is there something similar to a namespace that can be made into an array?

The namespace, if it helps, contains these variables:

const int maxx=// depends on the particular namespace
               // I need an array to go through each namespace and
               // pick out the variable
const int maxy=// depends on particular namespace
//prgm is a class I made
prgm sector[maxx][maxy];
// another array of prgms.  int is my shorthand of saying "depends on
// particular namespace", so is char.
prgm programs[int]={prgm1(int,int,char),prgm2(int,int,char)...

So any help would be welcome.

Was it helpful?

Solution

I am not sure what language you are talking about, but in many (most?) languages, references to constants are replaced by the constant value at compile time. So they are no longer present at runtime and even reflection won't help.

You could create a class in each namespace that exposes the constants as (static) properties. Then you can use reflection to search the class in each namespace and obtain the constant values from the properties.

But, as mentioned by others, you should really rethink your design. Finally, namespaces are usually not accessable via reflection because they just extend the class names of the contained classes (and other stuff). Or is there a (non-esoteric) language that exposes namespaces as entities via reflection?

For .NET the reference for the System.Type.Namespace property states the following.

A namespace is a logical design-time naming convenience, used mainly to define scope in an application and organize classes and other types in a single hierarchical structure. From the viewpoint of the runtime, there are no namespaces.

OTHER TIPS

You could use reflection, but I think you should rethink your design.

Is this supposed to be C++? Sounds like you need to define a class, not a namespace, then create instances (objects) of that class and put them in an array.

So the sector variable gets tricky, since it is sized based on the value of maxx and maxy parameters that would be passed to the constructor of the class. You can take care of that problem by using a container class or a dynamically-allocated multi-dimensional array instead.

If you talk about C++, in there you can't pass namespaces as entities around. But you can do so with types, as type argument to templates. In this case, an MPL sequence could help together with MPL algorithms:

struct c1 { typedef int_<2> value_x; }; 
struct c2 { typedef int_<3> value_x; };
struct c3 { typedef int_<1> value_x; };
template<typename C> struct get_x : C::value_x { };

typedef vector<c1, c2, c3> scope_vec;

typedef max_element<
    transform_view< scope_vec , get_x<_1> >
    >::type iter;

You may then create your array like

prgm programs[deref< iter >::type::value];

Note that the search within that type-vector happens at compile time. So the value of the array is determined at compile time either.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top