I have the following example code:
class A {
public:
static int a;
};
int A::a = 0;
class B {
public:
static A a1;
};
A B::a1;
class C {
public:
static A a1;
};
A C::a1;
int main(int argc, const char * argv[]) {
C::a1.a++;
B::a1.a++;
std::cout << B::a1.a << " " << C::a1.a << std::endl;
return 0;
}
Class B and C have class A as a static member variable.
I expected the program to print "1 1", however it prints "2 2".
If multiple classes have a static variable in common, are they shared (within the same scope?)
B b1,b2andC c1,c2, c3. – MSalters 5 hours ago