Navigation  without Java Scripts

Constructors and Destructors

Constructors are used to create objects. When an object is not needed any longer, it should be destroyed by a destructor to free the resources allocated to the object.

For objects destroying a special destructor delete is used.

O = demo::new, % creating
O:hello,
O:delete. % destroying
There might be desirable to initialize facts to special values, create a window on the screen, open a file, etc. In the same way, the programmer may wish to control, how an object is deleted, for instance closing windows or files.

For this purpose, in a class declaration section, it is possible to declare an explicit constructor(s) or destructor(s) for objects of the class.

Adding the predicate new to a class makes an explicit constructor for the class objects.   

Adding the predicate delete to a class makes an explicit destructor for the class objects.

Predicates new and delete must be procedures.

See also