[chuck-users] Doing something to all members of a class?
wheeler at kde.org
Tue Mar 13 10:53:01 EDT 2007
> Say I have a class that has a member that's simply a integer used for
> counting something.
> I'll have a few instances (ten or so) and they will all be counting
> their own little thing on their own. So far so good. Now something
> happens and I'd like to reset all the counters to zero after which
> they should be independant again.
> How to go about this in a efficient way?
Here's an example implementation:
ListNode @ next;
Foo @ item;
static ListNode @ first;
static ListNode @ last;
if(first == null)
new ListNode @=> first;
first @=> last;
this @=> first.item;
new ListNode @=> last.next;
last.next @=> last;
this @=> last.item;
0 => int value;
fun void increment()
fun void print()
<<< value >>>;
fun static void reset()
for(first @=> ListNode @ node; node != null; node.next @=> node)
0 => node.item.value;
The first part is just an outline of a linked list. Items are inserted
into the list as they are created. Then in the static function we go
through the list and modify each instance.
The conceptual problem that you had is that static functions operate on
*no* instance of a class rather than *all* instances of a class. Static
functions can only access static variables since the others are not
available (since there's no instance there for them to access).
More information about the chuck-users