In ColdC there are two types of variables, local variables and object variables. An object variable is defined on the object, and can be accessed by any method defined on the same object. Object variables exist outside of the scope of methods. Local variables are defined within a method, with their scope being limited to that method.
If a variable is used in a method, but it is not defined as an argument
or a local variable (at the top of the method), it is assumed to be an object
variable. When the method is executed the interpreter looks on the object
for the variable. If the interpreter cannot find the variable on the object,
the error ~varnf is thrown.
It is important to remember that variables may only be accessed by methods defined on the same object as the variable. No other method may reference that variable. As an example let us define two objects (if this syntax is unfamiliar, see the TextDB format):
object $obj_A: $root;
var $obj_A text;
public method $obj_A.get_text {
return text;
};
public method $obj_A.set_text {
arg new_text;
text = new_text;
};
object $obj_B: $obj_A;
Calling $obj_A.set_text("text") will set text
on $obj_A with a value of "text". $obj_B does inherit the
ability to define and write to it's own copy of text (using
methods defined on $obj_A), but it does not inherit values
assigned to object variables on it's ancestors. I.e. calling $obj_B.get_text() will return 0 (the default unset value), as we
have not yet set it to any value on $obj_B, only on
$obj_A (this is called encapsulation).
If we were to override the method get_text defined on
$obj_A, on $obj_B, as:
public method $obj_B.get_text {
return "more text: " + text;
};
When we called it, the ~varnf error would be raised, because
the object variable text is not defined on $obj_B, where the
overriding method get_text is defined.
Last Modified on Jan 25 1996
Copyright © 1995, 1996, Brandon Gillespie