static function/variable

前端 未结 3 1646
太阳男子
太阳男子 2021-01-28 01:28

i just started programming in c++ this concept of static variable/function wasn\'t clear to me..why is this used and is there any other alternative of this!!!

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-28 01:33

    static can be a little confusing because of its slightly different meaning depending upon where it is used.

    A static variable, declared globally, will only be visible in that source file.

    A static variable, declared locally, will keep its value over successive calls to that function (as in netrom's example)

    A static member variable does not belong to the class.

    A static function, declared in a class, can be used without declaring an instance of that class.

提交回复
热议问题