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 A
is a non-template class andA::foo()
is a non-template function. There will be only one copy ofstatic int i
inside the program.Any instance of
A
object will affect the samei
and lifetime ofi
will remain through out the program. To add an example:The keyword
static
unfortunately has a few different unrelated meanings in C++When used for data members it means that the data is allocated in the class and not in instances.
When used for data inside a function it means that the data is allocated statically, initialized the first time the block is entered and lasts until the program quits. Also the variable is visible only inside the function. This special feature of local statics is often used to implement lazy construction of singletons.
When used at a compilation unit level (module) it means that the variable is like a global (i.e. allocated and initialized before
main
is run and destroyed aftermain
exits) but that the variable will not be accessible or visible in other compilation units.I added some emphasis on the part that is most important for each use. Use (3) is somewhat discouraged in favor of unnamed namespaces that also allows for un-exported class declarations.
In your code the
static
keyword is used with the meaning number 2 and has nothing to do with classes or instances... it's a variable of the function and there will be only one copy of it.As correctly iammilind said however there could have been multiple instances of that variable if the function was a template function (because in that case indeed the function itself can be present in many different copies in the program). Even in that case of course classes and instances are irrelevant... see following example:
Simplified answer:
Static variables, regardless whether they are members of a (non-templated)
class
or a (non-templated) function, behave - technically - like a global label which scope is limited to theclass
or function.