roxen.lists.pike.general

Subject Author Date
Global / static variables? Linda Messerschmidt <linda[dot]messerschmidt[at]gmail[dot]com> 25-01-2009
One thing I've run into a couple of times now is the idea of a class
static or just a pure global variable.  I am kind of "getting" that
Pike's class/program model doesn't have a global scope in the
traditional sense, so I'm mostly looking at class/module shared
variables.

I've got several cases where I want to share data between all
instances of a class.  Here's a simple example:

>>> Countme.pike
static int sm_iCount = 0;
int m_iCount;

void create() {
	m_iCount = sm_iCount++;
}

void display() {
    write("Count = " + m_iCount + "/" + sm_iCount + "\n");
}
<<<

>>> main.pike

Countme s1 = Countme();
Countme s2 = Countme();
Countme s3 = Countme();

int main() {
	s1.display();
	s2.display();
	s3.display();
	return 0;
}
<<<

>>> Actual output
Count = 0/1
Count = 0/1
Count = 0/1
<<<

>>> Desired output
Count = 0/1
Count = 1/2
Count = 2/3
<<<

I know that "static" doesn't mean the same thing  in Pike as it does
in C++, so am I using it improperly here; it's just what I would do in
C++.

What is the best way to get the desired behavior/output?

I think this has something to do with .pmod vs. .pike, but the
documentation isn't super-clear on this point.  (In fact, in one place
it says "TODO: explain the difference between .pmod and .pike." :) )

Thanks!

-LM