Hi,
I have a question regarding defining an Array within a class. I would like to define the Array size by one of the Class' variables.
For example:
One Class will work with 2 Output Pins, a second will work with 3 Pins. How can I define the size of the Array based on if it needs to be able to hold 2 or 3 variables?
const byte MaxClstrSize = 3; // Define Maximum Array Size for Array in Class below
class Glow_Class { // defining a Class
public: // Class Member Variables. Initialized at startup
byte ClstrPinsArr[MaxClstrSize]; // Define an Array with the amount of 3 Max Members. If it only holds 2 pins, I'm wasting memory.
// How can I define this Array based on clstrSize?
float ClstrSpeed;
byte Pins;
public: // Constructor - Creates a Glow_Class and initializes the member variables and state
Glow_Class( float cSpeed, byte clstrSize, byte pins[]){
for (byte i = 0; i < (clstrSize); i++)
{
ClstrPinsArr[i] = pins[i]; // load pins into Array. For clstSize 2, it leaves i=2 unchanged.
}
ClstrSpeed = cSpeed;
}
void Update() {
// Do something...
}
};
//Cluster Size
byte Clstr_Size_1 = 2;
byte Clstr_Size_2 = 3;
// Define Pins
byte Clstr_Pin_1[2] = {1, 2}; // Array has 2 members --> Clstr_Size_1 will be 2
byte Clstr_Pin_2[3] = {3, 4, 5}; // Array has 3 members --> Clstr_Size_2 will be 3
Glow_Class Grp_1 ( 1, Clstr_Size_1, Clstr_Pin_1); // Define Grp_1
Glow_Class Grp_2 ( 1, Clstr_Size_2, Clstr_Pin_2); // Define Grp_2
void setup() {
}
void loop() {
Grp_1.Update(); // Update Classes
Grp_2.Update();
}
Currently I'm "cheating" by defining the Array with the maximum amount of variables it might need to hold (in this case 3). If it will need it or not. But this seems like a waste of memory. (Especially if some Grps might write to 10 pins, others only to 2... In a case like this I wouldn't want to define all the Arrays with a size of 10 members)
In the above example, is it somehow possible to define the Array based on the variable clstrSize?
I'm sending a different clstrSize with Grp_1 and Grp_2? (clstrSize for Grp_1 = 2, clstrSize for Grp_2 = 3)
I hope I'm making sense here. I'm happy to try and explain better if it's not clear
Thanks for your help and time.