When creating an array like in the sketch
#include <Lib.h>
int myArray[Lib::len];
void setup() {}
void loop() {}
the variable len must be an integer constant that is known at compile time. Like shown in the sektch, I want to put the length of the array into a library and get it from there. The problem is that for this to work, you have to initialize the constant already in the .h file, which is against proper library standards. This is how you would do this (.h file of the library):
class Lib {
public:
static const int len = 4;
};
The .cpp file doesn't even have to contain anything in this case. The proper way to do things in a library would be like this though:
The .h file:
class Lib {
public:
static const int len;
};
The .cpp file:
#include "Lib.h"
const int Lib::num = 4;
But if done like this, len
doen't have a value at compile time since the sketch only includes the .h file and not the .cpp file.
Is there any way to intitialize an array in a sketch with its length coming from a library while maintaining proper library design?