Node:Instance creation and slot access, Next:, Previous:Class hierarchy and inheritance of slots, Up:Inheritance



5.4.2 Instance creation and slot access

Creation of an instance of a previously defined class can be done with the make procedure. This procedure takes one mandatory parameter which is the class of the instance which must be created and a list of optional arguments. Optional arguments are generally used to initialize some slots of the newly created instance. For instance, the following form

(define c (make <complex>))

will create a new <complex> object and will bind it to the c Scheme variable.

Accessing the slots of the new complex number can be done with the slot-ref and the slot-set! primitives. Slot-set! primitive permits to set the value of an object slot and slot-ref permits to get its value.

(slot-set! c 'r 10)
(slot-set! c 'i 3)
(slot-ref c 'r) => 10
(slot-ref c 'i) => 3

Using the describe function is a simple way to see all the slots of an object at one time: this function prints all the slots of an object on the standard output.

First load the module (oop goops describe):

(use-modules (oop goops describe))

The expression

(describe c)

will now print the following information on the standard output:

#<<complex> 401d8638> is an instance of class <complex>
Slots are:
     r = 10
     i = 3