[chuck-users] Doing something to all members of a class?
Veli-Pekka Tätilä
vtatila at mail.student.oulu.fi
Tue Mar 13 06:55:45 EDT 2007
Hi Kassen,
Replying in this thread first, as I am still trying to learn enough ChucK to
contribute more in our other thread about seqs and such. THis is based on my
object-oriented knowledge in general and might be incorrect in the context
of ChucK. You have been warned, <smile>.
First of all, I think the subject is slightly ambiguous. In programming
jargon it could mean one of two things:
Members of a class as in the data members e.g. doing something to all data
fields of a particular object or class gloablly via reflection. OR what you
ment doing something to every instance of a class.
> Say I have a class that has a member that's simply a integer used for
> counting
> I'll have a few instances (ten or so) and they will all be counting their
> own little thing on
Hmm if it is their own thing, it is an instance i.e. non-static variable,
not a class variable. A class variable is like a global variable, except
that it is restricted in a class. In other words, all the count methods
would modify the same variable if the variable is a static variable. This is
assuming ChucK's static works like in Java, I'm not sure about that.
> happens and I'd like to reset all the counters to zero after which
The easiest way to do this would be to have a class method named
resetCounter and calling it for each of the instances you have in a loop.
Alternatively, you could keep track of all the instances in a static array
in the class, and then call the reset method for all the elements in the
array. But this latter thing is more complicated and might differ garbage
cllection, if it is based on reference counting the objects.
> thought that if I'd call a static function as a member of the class
Static functions are always called on a class, not on an object. Unless you
pass in the object as a parameter but that's suspicious outside of certain
scripting languages whose names start with p, in which it is the right way
to do it.
> then this function would be executed for all instances.
It can modify a variable that is common to all instances, that is they all
have the same copy and refer to the same variable, but it does not
automagically modify anything instance specific as it has no object to work
with, only the class data that's global to all objects.
> referencing non-static int member of th class inside of a static function
That is because chucK does not know hwich object's variable you are
accessing. THink of it like this:
If you call a non-static method, the variables you are likely to deal with
are particular to that instance of the class, although you can access and
modify static variables that all the instances share, too.
If you call a static method, you are doing something global to all
instances, such as modifying a shared static variable. AS the variable is
shared, you don't have to have an instance to be able to refer to it.
To illustrate further consider one of those scripting languages I hinted at,
such as Perl. In Perl your class or object methods always get a first
parameter, if they are called correctly. That argument is an object
reference, if it is an object method. ELse it is just a class name string
Which can generally only be used to refer to the variables that all
instances share i.e. class variables.
As an example say we have a person class with name and age as well as a
static variable called headCount. Each instance of the class has its own age
and its own name, but the member or datafield, if you will, headCOunt is
common to all instances. Modifying the headCount, whether it is in a static
method or not, wil change a single variable in the class and that change
will show up everywhere in that class's methods, because it is just a single
variable.
> about a nullpointer exception.
A semantic quibble, why is it called a null pointer if the language only
supports references? I've been thinking the same about Java.
Hope this clarifies matters. IF not, I could try to demonstrate using a code
sample.
--
With kind regards Veli-Pekka Tätilä (vtatila at mail.student.oulu.fi)
Accessibility, game music, synthesizers and programming:
http://www.student.oulu.fi/~vtatila/
More information about the chuck-users
mailing list