Scope and instances

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.

So that while loop, are you trying to say while something is true?

 while (1)

It's just going to lock into that running the two play functions. In addition, there will be a function within that while loop that runs the "chores" of the system which includes the user interface. If the user selects a different mode or anything like that, a return will be called to totally deconstruct everything and start over to re construct new instances for the new user choices.

At the basic level, I can't figure out how to create an instance for a user selected class that isn't global. Is there maybe a way to instantiate a class globally at a time other than start up and then somehow deconstruct it when needed as well? Is it possible to create an array of classes (not objects/instances)? I'm not sure what the right question is anymore.

Ok I’m not entirely sure what is going on in your program as far as mode 1 and mode 2 goes however after looking at it again I am pretty sure your syntax is messed up with the switch case. I’m going to attach a example of one I was working on a while ago just so you can see the syntax. I believe what you are trying to do (correct me if I’m wrong) is use the first 2 cases in the switch to set up specific variables and then enter a while loop, and use the other 2 switch cases to control a process right? Let me know if this helps at all.

switch ( mode 1 ) {

     case 0:
         do something;
         break;

     case 1:
         do something;
         break;
}

while ( something = 1 ){

     switch ( mode 2 ){
         case 0:
              do something;
              break;
         case 1:
              do something;
              break;
     }
}

here is the example I used

	switch (m_suit){
		//{ Invalid, Clubs, Diamonds, Hearts, Spades}
		case Invalid:
			cout  << "\n" << m_points << " of " << "Invalid ";
			break;
		case Clubs:
			cout  << "\n" << m_points << " of " << "Clubs ";
			break;
		case Diamonds:
			cout << "\n" << m_points << " of " << "Diamonds ";
			break;
		case Hearts:
			cout  << "\n" << m_points << " of " << "Hearts ";
			break;
		case Spades:
			cout  << "\n" << m_points << " of " << "Spades ";
			break;
		default:
			cout << "\nProcessing Error\n";
			break;
	}