E2082 TYPEOF can only be applied to object types with a VMT (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

This error message is issued if you try to apply the standard function TypeOf to an object type that does not have a virtual method table.

A simple workaround is to declare a dummy virtual procedure to force the compiler to generate a VMT.


program Produce;

type
  TMyObject = object
    procedure MyProc;
  end;

procedure TMyObject.MyProc;
begin
  (*...*)
end;

var
  P: Pointer;
begin
  P := TypeOf(TMyObject);    (*<-- Error message here*)
end.

The example tries to apply the TypeOf standard function to type TMyObject which does not have virtual functions, and therefore no virtual function table (VMT).


program Solve;

type
  TMyObject = object
    procedure MyProc;
    procedure Dummy; virtual;
  end;

procedure TMyObject.MyProc;
begin
  (*...*)
end;

procedure TMyObject.Dummy;
begin
end;

var
  P: Pointer;
begin
  P := TypeOf(TMyObject);
end.

The solution is to introduce a dummy virtual function, or to eliminate the call to TypeOf.