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开发者_如何学Go indicate otherwise.
Since class A
is a non-template class and A::foo()
is a non-template function. There will be only one copy of static int i
inside the program.
Any instance of A
object will affect the same i
and lifetime of i
will remain through out the program. To add an example:
A o1, o2, o3;
o1.foo(); // i = 1
o2.foo(); // i = 2
o3.foo(); // i = 3
o1.foo(); // i = 4
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:
#include <stdio.h>
template<int num>
void bar()
{
static int baz;
printf("bar<%i>::baz = %i\n", num, baz++);
}
int main()
{
bar<1>(); // Output will be 0
bar<2>(); // Output will be 0
bar<3>(); // Output will be 0
bar<1>(); // Output will be 1
bar<2>(); // Output will be 1
bar<3>(); // Output will be 1
bar<1>(); // Output will be 2
bar<2>(); // Output will be 2
bar<3>(); // Output will be 2
return 0;
}
Static variables inside functions
Static variable is created inside a function is stored on program’s static memory not on the stack.
Static variable initialization will be done on the first call of the function.
Static variable will retain the value in multiple function calls
Lifetime of the static variable is Program
Examples
#include <iostream>
using namespace std;
class CVariableTesting
{
public:
void FuncWithStaticVariable();
void FuncWithAutoVariable();
};
void CVariableTesting::FuncWithStaticVariable()
{
static int staticVar = 0; //staticVar is initialised by 0 the first time
cout<<"Variable Value : "<<staticVar<<endl;
staticVar++;
}
void CVariableTesting::FuncWithAutoVariable()
{
int autoVar = 0;
cout<<"Variable Value : "<<autoVar<<endl;
autoVar++;
}
int main()
{
CVariableTesting objCVariableTesting;
cout<<"Static Variable";
objCVariableTesting.FuncWithStaticVariable();
objCVariableTesting.FuncWithStaticVariable();
objCVariableTesting.FuncWithStaticVariable();
objCVariableTesting.FuncWithStaticVariable();
objCVariableTesting.FuncWithStaticVariable();
cout<<endl;
cout<<"Auto Variable";
objCVariableTesting.FuncWithAutoVariable();
objCVariableTesting.FuncWithAutoVariable();
objCVariableTesting.FuncWithAutoVariable();
objCVariableTesting.FuncWithAutoVariable();
objCVariableTesting.FuncWithAutoVariable();
return 0;
}
Output :
Static Variable
Variable Value : 0
Variable Value : 1
Variable Value : 2
Variable Value : 3
Variable Value : 4
Auto Variable
Variable Value : 0
Variable Value : 0
Variable Value : 0
Variable Value : 0
Variable Value : 0
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 the class
or function.
精彩评论