3.1.3 Instance data

An object combines state and behavior. The state of an object, often referred to as its instance data, is a collection of data operated on by imperative code (the behavior). The most natural way to represent instance data in C is as a structure, and this is the approach taken here. When an object is instantiated, memory is allocated for the instance data on the heap. (Many languages with built-in object models, such as C++, also allow objects to be allocated statically on the call stack. Stack-allocated objects are not considered in this thesis.)

BinaryTreeNode_Create() and BinaryTreeNode_Destroy() in Listing 3.2 and Listing 3.3 serve as constructor and destructor, respectively, hiding the means used to allocate and deallocate space for the instance data of objects. In this example, all instance variables of the BinaryTreeNode class are accessible only to the class implementation (as indicated by their private Java access specifiers). As a result, the members of the structure are hidden in the implementation file, conceptually inaccessible from other code.

Operations operate on the instance data, and thus need a way to reference it. A reference to the instance data is given as the first argument, which is normally hidden in languages that natively support object-orientation, such as C++. (Component Pascal is one of the few such languages that makes no attempt to hide this argument (Szyperski et al. 2002:331).) This argument is known as this or self; in Listing 3.2 and Listing 3.3, this argument is named pThis.