Memory Allocation of Static Members in a Class

前端 未结 3 1981
别跟我提以往
别跟我提以往 2020-12-17 01:40

I posted a question recently: Initialization of Static Class members.

Now please check this code:

#include
class A
{
    static int o         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 01:50

    The member obj_s is static this means it is defined independently of the class.

    The line:

    int A::obj_s=0;
    

    Not only defines its value but also defines the memory it uses.
    That is why in the previous question you were getting a linker error (no space had been allocated for 'obj_s`. Now that you have added the line the compiler is defined the memory location(s) used by the object and the program now links.

提交回复
热议问题