Can someone please explain how static variables in member functions work in C++.
Given the following class:
class A {
void foo() {
static int i;
i++;
}
}
If I declare multiple instances of A, does calling foo() on one instance increment the static variable i on all instances? Or only the one it was called on?
I assumed that each instance would have its own copy of i, but stepping through some code I have seems to indicate otherwise.
Since
class Ais a non-template class andA::foo()is a non-template function. There will be only one copy ofstatic int iinside the program.Any instance of
Aobject will affect the sameiand lifetime ofiwill remain through out the program. To add an example: