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 {
publ
I would suggest you to use std::map<int, std::string>
(or unordered_map
if you have C++11 support) instead of the array. You can then insert into this map with the code : m[67] = "Sun"
and retrieve items using std::string s = m[67];
.
Here's one old-school approach:
class NameArray {
public:
NameArray()
{
array[67] = "Sun";
array[68] = "Moon";
}
const char *operator[](size_t index) const
{
assert(index<256);
return array[index];
}
private:
const char * array[256];
};
class Foo {
public:
static NameArray name;
};
NameArray Foo::name;
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.