Thread: Static variables

  1. #1
    Registered User ashughs's Avatar
    Join Date
    Oct 2006
    Location
    Toronto, Canada
    Posts
    2

    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?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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
    Last edited by ZuK; 10-26-2006 at 07:17 AM.

  4. #4
    Registered User ashughs's Avatar
    Join Date
    Oct 2006
    Location
    Toronto, Canada
    Posts
    2
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 07-09-2007, 04:49 AM
  2. Static variables + Initialisation
    By kris.c in forum C Programming
    Replies: 2
    Last Post: 07-08-2007, 02:16 AM
  3. static variables
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2006, 06:35 AM
  4. Visual C++ 6/.net Debug static variables
    By neandrake in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 03:45 AM
  5. Replies: 5
    Last Post: 11-19-2002, 07:49 PM