Static variable to time struct

Hey all,
In a function, I have a static variable that I want to assign the time in seconds when a certain condition is met and keep that value until a different condition is met. The time value is a struct. Since now->sec is always incrementing, will timeWhenEventMadeActive below hold onto the initial value or will it increment everytime the function is called? I cant seem to test this.

1
2
3
4
5
6
7
8
9
10
11
12
static time_t timeWhenEventMadeActive = 0;
static bool initTime = 0;
if (!initTime)
{
  timeWhenEventMadeActive = now->sec; //holds uptime value in seconds
  initTime = 1;
}
   // some code.....

if((int)isOff == 1)
  initTime = 0; // tries to reset timeWhenEventMadeActive
Last edited on
You want a static variable? Then it's value initialised once during the first run of the method and it'll not be re-initialised again ever.

e.g
1
2
3
4
5
6
7
8
9
10
11
12
void add_one_and_print() {
 static unsigned i = 0;
 i++;
 cout << "i is now " << i << endl;
}

int main() {
 add_one_and_print();
 add_one_and_print();
 add_one_and_print();
 return 0;
}
Topic archived. No new replies allowed.