Previous: 2.2.13.7. Virtual Methods To the Table of Contents Next: 2.2.13.9. Fail procedure
2.2.13.7. Virtual Methods Table of Contents 2.2.13.9. Fail procedure

- 2.2.13.8. -
Table of Contents
2. TMT Pascal Language Description
2.2. Pascal Language Structure
2.2.13. OOP Extensions
2.2.13.8. Constructors


2.2.13.8. 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: 2.2.13.7. Virtual Methods To the Table of Contents Next: 2.2.13.9. Fail procedure
2.2.13.7. Virtual Methods Table of Contents 2.2.13.9. Fail procedure

- 2.2.13.8. -