Global Variable booleans question

Ok so I know in general its a really bad idea to use global variables but I have a situation where where 3-4 big functions that do alot of processing are using them same variables (that I recreate in each one) its around 20 bools so its too many to pass to each function (I think) naming convention is exactly the same in each so I was thinking it would be much easier just to have them all as global variables (in a class?, havent used them before but I think this is correct).

If this is "ok" to implement, is there a way I can simply switch all the 20 or so booleans to false in a simple statement so at the start of every function , they are all reset.
I hate globals except in cases where the data you're dealing with is truly global (which usually is not the case). So I would not recommend making 20 global booleans.

Are these booleans all related? IE: do they control different parts of a common functionality? If so, a better approach would be to put them in a class... and hav the 3-4 functions just be member functions of that class.

Or if you're unfamiliar with classes, the same thing could more or less be done by putting all the booleans in a struct and then passing a single struct to a 3-4 functions.

is there a way I can simply switch all the 20 or so booleans to false in a simple statement so at the start of every function


If they all have unique names, there is no good way, no. If they were all in an array, then you could easily do it in a loop.

If they're part of a struct, there are ways to "cheat" and reset them all through raw memory manipulation functions like memcpy, but I strongly recommend against doing that.
Hmm ok I was probably a little unclear so maybe this will help :P

All of the bools are unique so lets say I just had colours


bool black = false;
bool white = false;
bool grey = false;

In each function I am declaring them again. Function one will turn certain bools on and off and process information later on depending on which ones are active, then it goes back to the main which calls second function which does exactly the same as the first function but obviously has different parameters and conditions but the principle is the same.

I was just thinking instead of declaring them all 3 times for the 3 functions it would be way easier just to have them in one "group" that the functions can all use, and have a line at the beginning of each function to reset the "group" of bools , if that makes sense?
I think I understood you correctly the first time... maybe I just wasn't clear with my answer. =P

Since your variables all have a unique name (ie, "black", "grey", etc), you cannot 'reset' all of them easily. You'd have to do it one at a time:

1
2
3
black = false;
white = false;
...



Instead, if you have them in an array... you can just loop through the array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
enum color
{
  black = 0,
  white,
  grey,
  ...
  count  // keep last entry so it marks the number of colors
};

bool colors[color::count];

// then instead of doing black=true... you could do:
colors[black] = true;

// and since they no longer have unique names... but are all accessed via a 'colors' array
//  you can loop through and reset them all very easily:
for(int i = 0; i < color::count; ++i)
    colors[i] = false;



The added bonus to this is that they no longer have to be global. You can pass the array to/from functions as a parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void function(bool* colorarray)
{

    if( whatever )
        colorarray[ blue ] = true;
    //...
}

int main()
{
    bool colors[ color::count ]; // the (nonglobal) array of colors

    function( colors );  // pass it to the function.  function can access all colors
       // does not need to be global. 
Topic archived. No new replies allowed.