Static variables in instance methods

前端 未结 5 1649

Let\'s say I have this program:

class Foo {
 public:
    unsigned int bar () {
        static unsigned int counter = 0;
        return counter++;
    }
};

int m         


        
5条回答
  •  梦谈多话
    2021-02-14 03:55

    You just need to grasp two things:

    1. Static variables are stored in static area of the executing program (which is same as that of global variable).
    2. Scope is limited by the general rules of parentheses.Additionally static variables have internal linkage.

提交回复
热议问题