static member variable when declared private

后端 未结 3 860
旧巷少年郎
旧巷少年郎 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:00

    The problem is not the definition, but the fact that in main() (that's not in the name scope of static_demo, and cannot see a being private), you do an assignment.

    The definition of a is what you did at global scope, with int static_demo::a;. You simply need an initializer, if you want a not to start with an undefined value.

    int static_demo::a = 1;
    

    will solve the problem.

    From than on, a can be manipulated only by functions in static_demo (of friend of it).

提交回复
热议问题