My ignorance on the matter :lol:
I have zero confidence with global or static class variables, I have to learn how to treat them properly
Think of your program as a book of instructions for some desk clerk working with paper and pencil. Each function is a page in the instruction book.
The desk worker has a blank space in the middle of his desk for a pile of sticky notes and a sheet of A4 to his left. He can also remember one value at a time that he does not have written down anywhere.
First we'll consider what local variables look like in this model, as you seem to be fairly comfortable with that.
I learned Object-Oriented programming through Java (and with Java vocabulary rather than C++ vocabulary), and learned C later, but am not really confident with C++ (which is what the Orbiter API uses), so my example code will be with C, but should generalize to C++.
Let's say you call a function defined as:
Code:
int foo(int bar, int baz, int blah)
{
int n = 0;
n = bar + baz + blah;
return n;
}
One would normally in this case write "return bar + baz + blah;", but I'm being a bit wordier than I have to be in order to make a few points.
Let's say that you call foo() from main like this:
Code:
int main()
{
/*code*/
int i = foo (1, 2, 3);
/*code*/
}
To make the function call, you write the following instructions for the clerk: "Put a new sticky note on the pile, write the label 'go back to:' on the first line of the sticky note, and write the number of this page in the instruction book next to it and the line number of this line. The write the label "return value" on the second line of the sticky note, and leave it blank. The write the labels "bar", "baz", and "blah" on the next available lines on the top sticky note, and write the values 1, 2, and 3, respectively, next to them. Then find foo() in the index, and go to the corresponding page number."
When you declare n (a local variable to foo), you tell the clerk "write the label n on the next available line on the top sticky note."
When you assign bar + baz + blah to n, you tell the clerk:
"Find the values written on the lines on the top sticky note labeled 'bar', 'baz', and 'blah' and add them. Write the result on the line labeled 'n'".
When you return the value of n from the function, you write the instructions: "Remember the value on the line labeled n. Then go to the page number and line number in the instruction book written on the line labeled 'go back to' on the sticky note in your hand, and throw the sticky note in your hand away".
When the desk clerk gets back to "main()", he sees the next instructions are: "label the next available line on the top sticky note 'i', and write the last number you remembered on that line".
So local variables are lines on the top sticky note. When you enter a new function, the clerk covers up the top sticky note with a new one and writes the variables and parameters for the new function on the new sticky note. When you exit a function, he remembers the return value and throws the sticky note away, uncovering the sticky note for the function you're returning to, then writes the return value into whatever variable you're assigning it into (or forgets it if you don't use the return value).
So now we get to global variables and static class variables.
My example code will be with C global variables, but the way I explain it should generalize to C++ static class variables.
Lets say you have the following code:
Code:
const int blah = 3;
int i;
int n = 0;
int main()
{
foo(1,2);
foo(2,4);
}
void foo(int bar, int baz)
{
i = bar + baz + blah;
n += i;
}
This is like the code we had earlier, but i and n have been moved from being local variables of main and foo to being global variables, and blah been moved from being a parameter of foo to being a global constant. Foo has void return type, because it now assigns into global variables instead of returning a value.
In our desk clerk model, the labels for the global variables (and the class static ones in ) are pre-written in ink on the sheet of A4 paper to the desk clerk's left. The value for i is blank, the value for n is pre-written in pencil, and the value for blah is pre-written in ink.
So now we get to the instruction book. Unlike for the sticky notes, the instructions never tell him to write a label for a line on the A4 sheet, because the labels are pre-printed. And they never tell him to write a value for blah, because that value is pre-printed, so he can't erase it and write a new one. Also, unlike for the sticky notes, he is never told to put another sheet of A4 on top of the current one, or to throw the current one away. The same sheet is always there and ready to use.
The call and return instructions are the same as before (except that he doesn't have to remember a return value for foo()). When we assign to i, the clerk's instructions say "Add the values on the lines labeled 'bar' and 'baz' on the top sticky note, and the line labeled 'blah' on the A4 sheet, and write the result on the line labeled 'i' on the A4 sheet". When we assign to n, the instructions say "Add the value on the line labeled 'i' on the A4 sheet and the line labeled 'n' on the A4 sheet, and write the result in the line labeled 'n' on the A4 sheet."
After the first invocation of foo(), the value of i is 6 and the value of n is 6. After the second invocation, the value of i is 9 and the value of n is 15.
Member variables for an object (in an object oriented language), or an instance of a struct (in plain C) are stored wherever the containing object/struct is stored, either on the sticky notes or on the A4.
So local variables keep state relevant to the execution of a function, global variables keep track of state relevant to the execution of the program as a whole, and member variables keep track of the state of an object, however long it lasts for.
I could also extend the desk clerk model to cover dynamically allocated variables (state that needs to be kept track of for less than the lifetime of the program, but needs to accessible regardless of which sticky note is currently on top of the pile) and pointers, which are variables that tell you where to find other variables (mostly where to find dynamically allocated variables, but sometimes other things), as well as things like calls to external APIs, but that will take a while, and you haven't said that you aren't confident with them.