-
Static variables
Can someone clarify something for me on how static variables work.
This is my situation:
I have a Class Screen, which has a member Field* ptrField.
I have a Class Field which has a char* str and a function int Field::edit().
I have a static int curPos in edit() which tracks what position in the string I am at.
Now if I declare static int curPos = 0; at the beginning, should this not set curPos to 0 each time I point to a new Field in my Screen class?
Basically what I am trying to do here is each time I am editing a field for the first time, it sets curPos to 0 (or beginning of the string). Each time the same field is editted, it should start from the last position it was at when edit() was exited.
So does static int curPos=0; set it to 0 the first time it is called in the program, or is it the first time it is called for each Field object. Cuz wouldnt ptrField[0].edit() be a different function in ptrField[1].edit? Or am I completely way off base here?
-
>So does static int curPos=0; set it to 0 the first time it is called in the program, or is it the first time it is called for each Field object.
A static local variable is only initialized once, when the definition is first reached.
>Cuz wouldnt ptrField[0].edit() be a different function in ptrField[1].edit?
No. This gets into implementation details, but multiple instances of a class will use the same member function definition.
-
if curPos is a static variable of the Field class then there is only one for all Field's ( that is the purpose of a static class variable ). If I do understand your description then what you want is a normal member variable that is initialized to 0 in the constructor and updated in the edit method.
Kurt
Edit: removed nonsense
-
Thanks ZuK.
That fixed my one problem....now I just gotta figure out why a field set as uneditable is being overwritten when I press F1.