views:

32

answers:

1

Lets say i have something like this coded

class Normal_Mode;
class Fast_Mode;
class File_Control; //handles all operations with reading/writing in file
class Main_Control {
 private:
 some_class *root; //all other classes need access to root pointer since there is all the data(binary tree)
 File_Control *c_file_control;
 Fast_Mode *c_fast_mode;
...
}

Main_Control::Main_Control ( int argc, char* argv[]) {
  ...
  if ( argc > 1 ) {
   c_fast_mode = new Fast_Mode(argc, argv[]);
  } else {
   c_normal_mode = new Normal_Mode();
  };
  ...
};

int main (int argc, char* argv[]) {
 Main_Control c_main_control(argc,argv);
 return 0;
}

Lets say user input had argc > 1 and i am happy doing stuff with users input in Fast_Mode class but when i am finished and want to write stuff to file or read something from file while in Fast_Mode. How do people in real world access File_control class?

Do they make some global array full with pointers to these kinda of classes who need only 1 instance.

Do they pass pointers to Fast_Mode and other classes so it can have it stored in private members for access.

or they construct/destruct such classes all the time depending on when it is needed.

And what do they do with such *root pointer where all the actual data is stored and lot of other classes needs to access it

Or my design ideas are completely wrong and people in real world do it some other way?

A: 

I don't really know what you are trying to achieve with this: if possible make you goal more clear but what I would say is that people would ususally create an abstract interface called 'Mode' and then have both Normal_Mode and Fast_Mode implement it. That way you could write the following code:

class Main_Control {
    private:
       some_class *root; //all other classes need access to root pointer since there is all the data(binary tree)
       File_Control *c_file_control;
       Mode *c_mode;
       ...
};

And then you could set it like:

if ( argc > 1 ) {
   c_mode = new Fast_Mode(argc, argv[]);
} else {
   c_mode = new Normal_Mode();
};

So you just put the common functions in the Mode interface so that they behave the same way but can be placed inside the same mode variable. To learn more about class inheritance and polymorphism look here. I hope that this answers your question.

Robert Massaioli