title: How to store const array in class?
body: <p>My OOP-style C++ application has a method (used in many .cpp files), that get two parameters: pointer to array and array size:</p>
  
  <pre><code>bool SendData(unsigned char* data, size_t size);
  </code></pre>
  
  <p>I have some arrays, that will never changes. And I want to define them, or make a consts, or something else, for doing this:</p>
  
  <pre><code>bool result1, result2;
  unsigned char arr2[] = {0x2, 0x5, 0x6};
  result1 = SendData(PREDEFINED_ARRAY_1, sizeof(PREDEFINED_ARRAY_1));
  result2 = SendData(arr2, sizeof(arr2));
  </code></pre>
  
  <p>I try to solve it, using constants - just write in header file:</p>
  
  <pre><code>static const unsigned char PREDEFINED_ARRAY_1[] = {0x1, 0x3, 0x5};
  static const unsigned char PREDEFINED_ARRAY_1[] = {0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
  </code></pre>
  
  <p>But get compiler errors about '{' and '}'. It's confuse me, becouse I can write:</p>
  
  <pre><code>static const unsigned char c = 10;
  </code></pre>
  
  <p>And all will be ok. I can initialize variables, but can't init arrays? It's strange. </p>
  
  <p>Yes, I know, that static variables should be initialized in cpp-files, but I have many files, that use this function with constants, so, as I know, It's a dead way. I show this code only to explain what I want. </p>
  
  <p>Also I don't want to write something like this in constructor (or special function) after definition in class:</p>
  
  <pre><code>PREDEFINED_ARRAY_1[0] = 0x1;
  PREDEFINED_ARRAY_1[1] = 0x3;
  PREDEFINED_ARRAY_1[2] = 0x5;
  </code></pre>
  
  <p>Because the number of arrays is about  100 andlength ~20-30 symbols per each. Too many code for simple thing. Or not?</p>
  
  <p>Then, I try to create special class:</p>
  
  <pre><code>class Array
  {
  public:
      void Set(unsigned char symbol, ...); // using va_args
      unsigned char* GetData();
      size_t GetSize();
  
  private:
      unsigned char* data;
      size_t size;
  };
  
  class MainClass
  {
  public:
      MainClass();
      void Send();
  
      Array PREDEFINED_ARRAY_1;
      Array PREDEFINED_ARRAY_2;
  };
  
  MainClass::MainClass()
  {
      PREDEFINED_ARRAY_1.Set(3, 0x1, 0x3, 0x5);
      PREDEFINED_ARRAY_1.Set(6, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7);
  }
  
  void MainClass::Send()
  {
      SendData(PREDEFINED_ARRAY_1.GetData(), PREDEFINED_ARRAY_1.GetSize());
  }
  </code></pre>
  
  <p>But in this case, I should manually count number of symbols. Maximum length may be about 50 symbols and the risk of mistake is to high. I want to avoid it. </p>
  
  <p>Third and dumbest way is something like this:</p>
  
  <pre><code>#define PREDEFINED_ARRAY_1 {0x1,0x3,0x5}
  
  int main()
  {
      unsigned char arr1[] = PREDEFINED_ARRAY_1;
      SendData(arr1, sizeof(arr1));
  }
  </code></pre>
  
  <p>But it is too ugly.</p>
  
  <p>My question is: <strong>What the best practice for doing it?</strong> </p>
  
  <p>P.S. As I already say, I'm using OOP paradigm and want to avoid global variables and other C-things.</p>