On Mon, 17 Sep 2007 10:25:01 +0200, Henrik GrubbstrXm (Lysator) @ Pike (-) developers forum 10353@lyskom.lysator.liu.se wrote:
Hi.
Some of you may have noticed that I implemented static variables with function scope a day or two ago. Example:
int inc() { static int counter; return counter++; }
The problem in Pike is that the intended lifetime of the variable isn't always obvious; consider nested classes:
class X { int inc() { static int counter; return counter++; } }
int main() { function(:int) X1 = X()->inc; function(:int) X2 = X()->inc; write("X1:%O,%O\n" "X2:%O\n", X1(), X1(), X2()); }
Should the above output X1:0,1 X2:0, or X1:0,1 X2:2? ie should the variable be stored in X or in the top-level class?
As I perceive it from other languages, 'static' means a really global (non object-oriented) memory location. Whenever int() is called from anywhere, be it different objects or functions, I would expect 'counter' to be shared. So, I would vote for the 'X1:0,1 X2:2' solution.
Bernd