Does C++ static member variable has different instance in a single process?

  c++

There is a C++ class has one static member variable, and two static methods like :

Class ABC
{
    static int val = 0;
    
    static void set_val(int v) {
          val = v;
    }
    
    static int  get_val() {
         return val;
    }
};

The Class is defined and implemented in a static library : libabc.a

I make a executable object file which link test.o and libabc.a

My question is, when I call ABC::set_val(10) in test.o, and call another function, abc_func() , in libabc.a

Then abc_func() call ABC::get_val() and got a 0.

Why is ABC::val not 10?

I thought the scope of a static member variable is the whole process.

I print the address of ABC::val, I got 2 different address in the functions of test.o and libabc.a.

Why? I thought test.o and libabc.a is in a same process, so the static member variable address should be the same.

Can anyone explain this?

Source: Windows Questions C++

LEAVE A COMMENT