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

If you try to use the standard function TypeOf on an object type that doesn’t have a virtual method table, you’ll get this error notice.

Declare a dummy virtual procedure to force the compiler to build a VMT as an easy workaround.

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 attempts to use the TypeOf standard function on a type TMyObject that lacks virtual functions and thus 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.

Introduce a dummy virtual function or remove the call to TypeOf as a workaround.