Technical Notes Database
TN436D.txt CONSTRUCTOR CALL WITHIN METHOD
Category :Pascal
Platform :All
Product :Turbo Pascal 5.5
Description:
Q. I am calling my constructor from within a method, why am I
having problems?
A. The problem will arise when the constructor loads the new VMT
pointer. It loads the pointer to the VMT for the constructor's
table, not the instances. Therefore if a descendant calls an
ancestor's constructor, the descendant's VMT will now point to
the ancestors VMT. The problem now occurs when the descendant
tries to call a method that was defined after the ancestor.
The VMT entry for this method is unknown. Look at the
following example:
Type
L1 = Object
Constructor Init;
Procedure First; Virtual;
End;
L2 = Object ( L1 );
Constructor Init;
Procedure Second; Virtual;
End;
Constructor L1.Init;
Begin
End;
Constructor L2.Init;
Begin
End;
Procedure L1.First;
Begin
Init;
End;
Procedure L2.Second;
Begin
Init;
End;
Var
L : L2;
Begin
L.Init; { This calls L2.Init and loads a pointer to }
{ the L2 VMT into L. }
L.First; { This will call L1.First, which in turn calls }
{ L1.Init because as far as the procedure is }
{ concerned, the Self pointer is a pointer }
{ to an object of type L1. }
L.Second; { This is undefined. Since the VMT now }
{ pointed to by L is L1's, the pointer to }
{ method Second is undefined. Therefore, the }
{ call to this method is undefined. }
...
***Note: This also applies to polymorphic procedures.
Reference:
7/16/98 4:35:32 PM
Last Modified: 01-SEP-99