class A
{
static void f(void)
{
int a;
static int b;
}
};
Is there any (formal or practical) difference between a
and b
?
class A
{
static void f(void)
{
int a;
static int b;
}
};
Is there any (formal or practical) difference between a
and b
?
Yes, consider the following:
#include <iostream>
using namespace std;
class A
{
public:
static void func()
{
static int a = 10;
int b = 10;
a++;
b++;
std::cout << a << " " << b << endl;
}
};
int main() {
A a, b;
a.func();
b.func();
a.func();
return 0;
}
a
is shared across all instances of func
, but b
is local to each instance, so the output is:
11 11
12 11
13 11
Yes both are different. For every call a
will be created whereas b
will be created only once and it is same for all the objects of type A
. By same I mean, all the objects share a single memory of b
.