Next: , Previous: , Up: obj-bas   [Contents][Index]


10.30.1.3 Using Classes

Given this definition, the following command creates an instance of the point class, assigning values to its x and y slots, and prints a description of the point.

| ?- create(point(3,4), PointObj),
     PointObj <- print(user_output).

The print message prints (3.0,4.0). The variable PointObj is bound to a Prolog term of the form

point(Address)

where Address is essentially a pointer to the object.

In general, an object belonging to a class ClassName will be represented by a Prolog term of the form

ClassName(Address)

The name ClassName must be an atom. This manual refers to such a term as if it were the object, not just a pointer to the object. Users are strongly discouraged from attempting to do pointer arithmetic with the address.

After execution of this command, the point object still exists, but the variable PointObj can no longer be used to access it. So, while objects resemble clauses asserted into the Prolog database in their persistence, there is no automatic way to search for an object. Objects are not automatically destroyed when they are no longer needed. And, there is no automatic way to save an object from one Prolog session to the next. It is the responsibility of the programmer to keep track of objects, perhaps calling the destroy/1 predicate for particular objects that are no longer needed or asserting bookkeeping facts into the Prolog database to keep track of important objects.


Send feedback on this subject.