can someone exlpain this code to me
size_t count_calls() {
static size_t ctr = 0; // value will persist across calls
return ++ctr;
}
int main() {
for (size_t i = 0; i != 10; ++i)
cout << count_calls() << endl;
}
how can ctr not restet to zero after calling the count calls function many times? As you can see the object ctr intelizes to 0 after the function call. shouldnt ctr be 0 when reentering the function count_calls as it passes the first line static size_t ctr = 0;?
I understand that a static variable holds its value thoughout the function calls until the program terminates but here, every time the functions is called shouldnt the first linee reset the the ctr value to zero or does the intialization work only the first time and thus become ignored the rest of the calls?
can someone please explain how this works?