1

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};
1
  • [SIZE-4]=1 are called designated intializers and are only available in C and illegal in C++ (however, in gcc they offer it as an extension). However, non-trivial (i.e. your example) are not supported. Commented Jul 4, 2012 at 4:14

2 Answers 2

4

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];.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but I'd like to do it without importing any more libraries if possible.
@GGG: It is part of the C++ standard library..no additional libraries are required.
This will be different than the rest of the code. Why do you suggest using the std::strings instead of const char *? The code is written in a c-like way, encapsulated in classes.
@GGG: You can use use const char*, I suggested std::string as people generally forget the const part of the char* and use it as a non char* pointer with functions like strcpy. That will cause the program to crash.
1

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.

5 Comments

Should NameArray be a singleton if I'm going to do this?
@GGG: If by singleton you mean removing the ability to copy or assign, sure if that is what you want.
I mean, this doesn't really have the advantage of being static, I was thinking a singleton pattern could fix that, but it seems like overkill. I really just want a plain static sparse array. Is there no way to do that?
@GGG: I'm not sure what you mean. It is still declared static in the Foo class.
Hmm, good point, I was thinking other things would need to instantiate NameArrays, but they would just look at Foo::name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.