Previous: Virtual Methods To the Table of Content Next: Fail procedure

- 2.2.13.8 -
TMT Pascal Language Description
Pascal Language Structure
OOP Extensions

Constructors

Constructors initialize (instantiate) objects by creating and filling their VMT. Any object that uses virtual methods must be first initialized.
type
  Circle = object
    Center: Point;
    Radius: Longint;
    constructor Init(Z: Point; R: Longint);
    procedure Show; virtual;
    procedure Hide;
    destructor Kill;
  end;

var
  C: Circle;
  P: Point;
The following code will instantiate, display, hide, and display the circle C:
  P.X := 20;
  P.X := 40;
  C.Init(P, 100);
  C.Show;
  while not KeyPressed do ;
  C.Hide;
  C.Kill;
where:
constructor Circle.Init(Z: Point; R: Longint);
begin
  Center := P;
  Radius := R;
end;
Besides initializing the fields of the circle C; C.Init also creates a VMT table. This table is essential for calling a virtual method, such as Show.

Without the C.Init call, the example above will fail (probably cause a run-time exception or halt the system). However, when the example compiled with the range-check {$R+} switch on, TMT Pascal will automatically detect calls from a non-instantiated method and produce a run-time error.

See also:
Destructors


Previous: Virtual Methods To the Table of Content Next: Fail procedure
Virtual Methods Table of Content Fail procedure

- 2.2.13.8 -