Cold Help System: Programming: ColdC Reference Manual: Objects: Variables

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. Object variables will always exist until they are explicitly removed. Local variables only exist while the method defining them is executing.

If a variable is used within the body of 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 object variables may only be accessed by methods defined by same object defining the variable. No other method may reference that variable. For instance, assume two objects exist as $obj_a and $obj_b, where $obj_b is a child of $obj_a. $obj_a defines the variable text and the methods set_text and get_text:

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;

Calling $obj_a.set_text("text") will set $obj_a's instance of the object variable text to "text". $obj_b does inherit the ability to use and write to it's own instance of text (using methods defined on $obj_a), but it does not inherit values assigned to the same object variable on it's ancestors. For instance, calling $obj_b.get_text() will currently return 0 (the default unset value), as it's instance of text has not yet been defined, even though $obj_a's instance of text has been defined. This is called encapsulation.

Calling $obj_b.set_text("more") would set $obj_b's instance of text to "more". If, following this, $obj_b were to override the method get_text defined on $obj_a as:

public method $obj_b.get_text {
    return "more text: " + text;

Calling $obj_b.get_text() would cause the error ~varnf to be thrown, rather than having "more text: more" returned. This is because the object variable text is not defined by $obj_b (where the new get_text is defined), even though it has it's own instance from $obj_a.

Another way to look at this is from a C++ perspective. All object variables in ColdC are equivalent to being private object variables in C++.

Referencing Objects | Methods | Variables | Special Object Status

the Cold Dark