static member variable when declared private

后端 未结 3 850
旧巷少年郎
旧巷少年郎 2020-12-24 08:27

When a static member variable is declared private in a class, how can it be defined?

Suppose i have the following class declaration

class static_dem         


        
3条回答
  •  一向
    一向 (楼主)
    2020-12-24 09:13

    in your .cpp:

    int static_demo::a(0);
    

    when this causes an error, it is most common that you either encountered a linker error for a multiply defined symbol (e.g. the definition was in the header), or that you may have tried to define it in the wrong scope. that is, if static_demo is in the namespace MON, it would be defined in the .cpp:

    #include "static_demo.hpp"
    int MON::static_demo::a(0);
    

    for other types, it may simply have been an incorrect constructor.

提交回复
热议问题