Asked my professor today and he seemed to say that Global variables in general are bad.
I already kinda knew this was the case, but he didn't really say much about static global variables.
My question: Can you make a static global variable, if so, does it have the same pitfalls as a regular global variable?
ie: one that only works for the said file?
My example:
Working on a project that sorts latin words in a tree alphabetically. If the same occurrence of a word happens, instead of the add() function just returning and doing nothing, we need to add +1 to some integer counter.
I wanted to add a line of code in my add() function, so that in the case of return, it would be like
Code
include <xxx>
// decs located in a .h file
int counter = 0;
int main {....
// this is the main .cpp file
____________________________________________________________
// this is a function definition
void add(string someString, .....)
{
...
if (word == somestring)
{
counter++;
return;
}
....
I wanted to do something like that. But he told me it would just be best to pass by reference. I already knew this was the way to go, but I am wondering if a static global
1) exists
2) serves any purpose without huge pitfalls.
This post was edited by Eep on Oct 24 2012 03:44pm