Objects

Strings, arrays and instances of user defined types are all different kinds of objects.

Objects are handled by reference. This means that the actual memory used by an object to store its data is shared between all the variables that refer to that object.

This makes objects very efficient to deal with, as assigning an object to a variable simply involves assigning a reference to the object - not an entire object. Passing objects to functions and returning objects from functions is also very efficient as again only references need to be copied.

BlitzMax uses a technique known as reference counting to keep track of objects that are in use by your program.

The basic idea behind reference counting is that each object includes a counter reflecting the number of variables referencing that object. When an object's counter reaches 0, the object is no longer in use and its memory can be returned to the system.

However, this process is not completely automatic, and BlitzMax requires you to execute the FlushMem command periodically in order to return unused memory to the system.

Objects can also have methods - function-like operations associated with the object. All objects, including strings and arrays, provide at least the following methods:

MethodDescription
ToString:String()Converts an object to a string. By default, this method returns the address of the object in memory, but user defined types can override this method to perform their own function.
Compare:Int( otherObject:Object )Returns a value less than 0 if an object is less than another object, a value greater than 0 if an object is greater than another object or the value 0 if an object equals another object. By default, the address of the objects in memory is compared, which is not very useful. However, user defined types can override this method to provide a more meaningful comparison.

Note the equals and not equals operators, = and <> do not use the compare method. These operators always compare memory addresses, allowing you to determine if 2 object references actually refer to the same object.

To access an object's methods, use the member access operator. For example:
Print "Hello".Compare( "Goodbye" )