I can't seem to figure out how to organize the flow of my program. I have a bunch of classes that are operating modes of my system. Classes are used because each mode can be run over a variable rectangular area on a Cartesian grid. Therefor, the constructors are declared with x1,y1,x2,y2 to define their operating area on the grid. I have around 14 classes and many are several hundred lines of code using a lot of variables. This means the instances must be constructed and destructed as they are needed (via user input). Finally, the user is able to select multiple (we will keep it at 2 for now) running modes that need to run simultaneously meaning there are 14x14 (for now) combinations. Explicitly defining all instances is both cumbersome and would overflow the stack many times over.
Below is the basic jist of what I need to accomplish. Each class has a constructor, sometimes a one shot init function (because some auxiliary functions don't seem to work in the constructor), and a play function that must be run continuously run.
The problem with the below version is the play functions go out of scope of the instance. How can I accomplish the same thing? Keep in mind the switch will have a minimum of 14 choices, all different classes.
void splitCourtRun()
{
switch (mode1)
{
case 0:
Rainbow rainbow1(1, 1, 3, yDim);
rainbow1.init();
break;
}
switch (mode2)
{
case 0:
Rainbow rainbow2(4, 1, xDim, yDim);
rainbow2.init();
break;
}
while (1)
{
switch (mode1)
{
case 0:
rainbow1.play();
break;
}
switch (mode2)
{
case 0:
rainbow2.play();
break;
}
//if something from user interface, return - destructing everything!
}
}
I have seen that you can declare an array of functions (pointers) using typedef but I don't know how that can be done with instances. I'm pretty new to classes/objects/instances and pointers so be gentle.