How to write an inheritated class containing an array

Hi,
i am working on a project, where i have a couple of classes that look basically like this: (the intention is writing a font for an led matrix)

class test1
{
  public:
    bool arr[3] = {0, 1, 0};
    void doSomethingWithArr()
    {...}
};

class test2
{
  public:
    bool arr[5] = {0, 0, 0, 1, 1};
    void doSomethingWithArr()
    {...}
};

so all classes share the same function but have arrays of different sizes.
I would like to write a parent class so that each child class only contains the array. However because the shared function does something to the content of the array, the array must be defined in the parent class or i cant get the programm to compile.

I tried to create a parent class like this:

class test_parent
{
  public:
    bool arr[];
    void doSomethingWithArr()
    {...}
}

but then the functions of all inheritated classes use the arr[] that was defined in the parent class and not the ones that are defined in the child classes.

How would i go about this problem?

Pass the pointer to the specific array to the parent function. Since the arrays are all different lengths, you will also need to give it the length:

  doSomethingWith(arr, len);

Another way to do it is to define the pointer in the parent but the constructor or begin() function of the child classes can malloc() a piece of memory and point the 'parent' pointer to it.

class TestParent {
  protected:
    int _arr[];
    int _length;
  public:
    void doSomethingWithArr();
};

class TestChild: public TestParent {
};

void TestChild::TestChild() {
  _arr = malloc(5 * sizeof(int));
  _length = 5;
}

void TestParent::doSomethingWithArr(){
}

so all classes share the same function but have arrays of different sizes.

The same function, or the same function name?

You can't create an array with no length specified. It's time you learned about the relationship between pointers and arrays.

The classes should declare arrays of the appropriate size. The base class should NOT declare an array.

The one method should take a pointer, of the appropriate type (which is likely NOT bool), and a length.

Each derived class can call the inherited method with its array and its array's length.

Thanks for your answers. I tried to do it with the memalloc function, but i i didnt really get it to work because the arduino array class cant assigning to an array from an initializer list so i would have to define the array already in the parent class. I'm also not sure how to do it without defining any array in the parent like Paul mentioned.

This is my full code of two individual font classes:

#ifndef fonts_matrix_h
#define fonts_matrix_h

class font5x3
{
  private:
    byte fontwidth = 3;
    byte fontheight = 5;
    bool n1[15] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1};
    bool n2[15] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1};
    bool n3[15] = {1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1};
    bool n4[15] = {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1};
    bool n5[15] = {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1};
    bool n6[15] = {1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1};
    bool n7[15] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1};
    bool n8[15] = {1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1};
    bool n9[15] = {1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1};
    bool n0[15] = {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1};

  public:
    font5x3() {}
    ~font5x3() {}
    //This Method gets as input an string of numbers (s) and a starting position s_x and s_y. c1 is the foreground color and c2 the background color.
    //The Method then goes through the input string and chooses the right array containing boolean pixeldata (1 = foreground color, 0 = background color)
    void drawNumber(cLEDMatrixBase &matrix, byte s_x, byte s_y, String s, CRGB c1 = CRGB::White, CRGB c2 = CRGB::Black) //Draw a  Number at x,y
    {
      bool * number;
      for (byte i = 0; i < s.length(); i++) //go through the input string s f.e. "10"
      {
        byte x = s_x + (fontwidth + 1) * i; //x,y drawing position  for the current number 
        byte y = s_y;
        switch (s[i]) //chose the right array for the current nuber
        {
          case '1': number = n1;
            break;
          case '2': number = n2;
            break;
          case '3': number = n3;
            break;
          case '4': number = n4;
            break;
          case '5': number = n5;
            break;
          case '6': number = n6;
            break;
          case '7': number = n7;
            break;
          case '8': number = n8;
            break;
          case '9': number = n9;
            break;
          default: number = n0;
            break;
        }
        for (byte column = 1, counter = 0; column <=  fontheight; column++) //Set the right pixels in the led matrix based on the array chosen above.
        {
          for (byte row = 1; row <= fontwidth; row++, counter++)
          {
            if (number[counter] == true)
              matrix(x + row - 1, y + column - 1) = c1;
            else
              matrix(x + row - 1, y + column - 1) = c2;
          }
        }
      }
    }
};

class font7x3
{
  private:
    byte fontwidth = 3;
    byte fontheight = 7;
    bool n1[21] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1};
    bool n2[21] = {1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1};
    bool n3[21] = {1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1};
    bool n4[21] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1};
    bool n5[21] = {1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1};
    bool n6[21] = {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1};
    bool n7[21] = {0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1};
    bool n8[21] = {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1};
    bool n9[21] = {1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1};
    bool n0[21] = {1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1};

  public:
    font7x3() {}
    ~font7x3() {}
    void drawNumber(cLEDMatrixBase &matrix, byte s_x, byte s_y, String s, CRGB c1 = CRGB::White, CRGB c2 = CRGB::Black) //Draw a  Number at x,y
    {
      bool * number;
      for (byte i = 0; i < s.length(); i++) //go through the input string s f.e. "10"
      {
        byte x = s_x + (fontwidth + 1) * i; //x,y drawing position  for the current number 
        byte y = s_y;
        switch (s[i]) //chose the right array for the current nuber
        {
          case '1': number = n1;
            break;
          case '2': number = n2;
            break;
          case '3': number = n3;
            break;
          case '4': number = n4;
            break;
          case '5': number = n5;
            break;
          case '6': number = n6;
            break;
          case '7': number = n7;
            break;
          case '8': number = n8;
            break;
          case '9': number = n9;
            break;
          default: number = n0;
            break;
        }
        for (byte column = 1, counter = 0; column <=  fontheight; column++) //Set the right pixels in the led matrix based on the array chosen above.
        {
          for (byte row = 1; row <= fontwidth; row++, counter++)
          {
            if (number[counter] == true)
              matrix(x + row - 1, y + column - 1) = c1;
            else
              matrix(x + row - 1, y + column - 1) = c2;
          }
        }
      }
    }
};

#endif //fonts_matrix_h

My original goal was to have a font_base class, so when i added more fonts later i wouldn't have to always write the drawNuber function.

Your drawNumber() method is not using a single array. It is using many different arrays.

If you used a 2D array, with one row for each digit, you could pass the 2D array to the method, along with the length of one dimension (the other is presumably fixed since you need to define the same number of characters in each font).

Then, the method wouldn't need access to member variables, so it really would be the same method in all classes, and could, therefore be part of a base class.

That method should NOT take a damned String. It should take a string - a NULL terminated array of chars.