[fpc-pascal]How to obtain runtime type of class object in debugger ?

Marco van de Voort marcov at stack.nl
Tue Feb 25 12:30:44 CET 2003


> Is it possible ?
> 
> In details:
> 
> Suppose:
> 
> program Test;
> 
> type
>   TClassA = class;
>   TClassB = class(TClassA);
>   TClassC = class(TClassA);
> 
> 
>   procedure P( x: TClassA);
> 
>   begin
>     ...
>   end;
> 
> begin
>   P(TClassB.Create)
> end.
> 
> If I watch x inside P in the IDE, i see it is of type TClassA.
> 
> But I want to know it's runtime type, that is if it is TClassB or TClassC.

Do you want to see it in the debugger? Then you need to try to examine
tobject properties or methods (like Anton said).

If you want to know it in a program, use the IS operator.

program Test;

type
  TClassA = class 	
	    end;
  TClassB = class(TClassA);
  TClassC = class(TClassA);



  procedure P( x: TClassA);

  begin
    if x IS TClassB then
     writeln('TClassB');
    if x IS TClassA then
     writeln('TClassA');
    if x IS TClassC then
     writeln('TClassC');
  end;

begin
  P(TClassB.Create)
end.

will print class B AND A (because it is also a TClassA)



More information about the fpc-pascal mailing list