Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If multiple classes have a static variable in common, are they shared (within the same scope?)

Tags:

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?)

like image 898
syko Avatar asked Oct 18 '16 02:10

syko


People also ask

Are static variables shared between classes?

A static variable acts as a global variable and is shared among all the objects of the class. A non-static variables are specific to instance object in which they are created. Static variables occupies less space and memory allocation happens once.

Are static variables shared?

Class Variables and MethodsA static variable is shared by all instances of a class. Only one variable created for the class.

Do static variables go out of scope?

Static variables offer some of the benefit of global variables (they don't get destroyed until the end of the program) while limiting their visibility to block scope. This makes them safer for use even if you change their values regularly.

Do static variables stay the same?

Since the static variables stay the same throughout the life cycle of the program, they are easy to deal with for the memory system and they are allocated in a fixed block of memory. Here is an example of static variables with different duration. All the static variables persist until program terminates.


2 Answers

The static members belong to class, it has nothing to do with objects.

Static members of a class are not associated with the objects of the class: they are independent objects with static storage duration or regular functions defined in namespace scope, only once in the program.

For your code, there's only one A::a, which is independent of B::a1 and C::a1 (which are objects of class A). So both B::a1.a and C::a1.a refer to A::a.

like image 188
songyuanyao Avatar answered Nov 15 '22 17:11

songyuanyao


You're not looking at multiple classes here. Both B::a1 and C::a1 are of type A. And A has a static variable a, that you accessed twice. If you also wrote A::a++, your program would have printed 3 3

To modify your example slightly:

struct A {     static int a;     int b; }; int A::a;  struct B {     static A a1; }; A B::a1{0};  struct C {     static A a2; }; A C::a2{0}; 

and the user code:

B::a1.a = 1; // A's static variable changed B::a1.b = 2; // B's A's b changed to 2 cout << B::a1.a << ",  " << B::a1.b << endl; cout << C::a2.a << ",  " << C::a2.b << endl; 

It will print:

1, 2 1, 0 

That's because all As share a, but all As have their own b. And both C and B have their own A (that they respectively share between objects of their type)

like image 26
krzaq Avatar answered Nov 15 '22 16:11

krzaq