As suggested in st2000’s answer, you could use dynamic allocation but, if you do that, do not forget to free the allocated memory in the destructor. Otherwise you have a nasty memory leak and your program will not live long:
class MyClass {
public:
MyClass(size_t length) : arrayLength(length) {
theArrayOfNumbers = new int[arrayLength];
}
~MyClass() {
deletedelete[] theArrayOfNumbers;
}
private:
const size_t arrayLength;
int *theArrayOfNumbers;
};
One issue with this approach is that dynamic allocation is no very friendly to the limited amount of memory you have in a microcontroller.
A safer option would be to use a template class instead. This way the array is statically allocated:
template <size_t arrayLength>
class MyTemplateClass {
int theArrayOfNumbers[arrayLength];
};
One caveat with this approach is that the users of the library may not be comfortable with the syntax for instantiating a template. You will have to provide clear examples in the documentation. Another issue is that the array size will have to be a compile-time constant.