I need to initialize a static array. Not all of the values are sequential.
Something like this works fine for a sequential array:
class Foo {
public:
static const char * name[];
}
const char * Foo::name[] = { "Sun", "Moon" };
How can I assign values at arbitrary positions in the array? I need to do something like this (pseudocode):
const char * Foo::name[] = { 67: "Sun", 68: "Moon" };
The array will never be bigger than 255; the indices come from byte values.
I found part of a thread where someone gives an example of something similar to what I want, but I couldn’t get anything like this to work.
type array[SIZE] = {[SIZE-4]=1, 2, 3, 4};
Here’s one old-school approach:
By wrapping the array in a class, you can make sure it gets constructed with the values that you want. You can also do bounds checking.