Hello. I'm writing a scheduler for a project using an ESP8266. I want to create a class called Task, and you can create a new Task like so:
void callback() {
Serial.println("callback called");
}
int time = 60000; // run callback in 60 seconds
Task myTask(time, callback);
My plan is to then create a function that checks all tasks to see which ones are completed, and run the callback from those.
I ran into some issues trying to pass the callback() function into the class and store it as a variable there. I'm new to C++, but I figured I might be able to store it as a pointer to the function, but I just couldn't get that working.
Does anybody know of a good way to pass and store the callback in the Task class?
Using the code above, I'm trying to store many Tasks in a single array, to cycle through them and check if they're completed. I can get them into an array by using the code below, but whenever I try to use sizeof() it returns 4, and whenever I try to use sizeof(arr)/sizeof(arr[0]) that just returns 0.
Task one(3000, function);
Task two(6000, function);
Task three(4000, function);
Task z[] = { one, two, three };
checkTasks(z);
void checkTasks(Task tasks[]) {
Serial.println(sizeof(tasks)); // always returns 4, regardless of actual length of array
}
4 is the number of bytes for a pointer which is really what you are asking for.
unfortunately when you pass an array to a function there is something known as array to pointer decay that happens and you loose the size of the array.
so one way to deal with it is to pass the size alongside the (decayed) array