Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2003-02

How to obtain runtime type of class object in debugger ?

6 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Olle Raab

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.

Olle

Reply by email

Marco van de Voort
to Olle Raab
Show quoted text 27 lines
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)

Reply by email

Jonas Maebe
to Anton Tichawa: Hello, Marco! What object code is generated for this? I assume that, informally speaking,…
Anton Tichawa wrote 14 lines
What object code is generated for this? I assume that, informally speaking, Ord(TClassB) = Ord(@VMT) and that if x IS TClassB yields somthing like if @(x.VMT) = TClassB? Am I wrong here?

Yes, because "is" also returns true if the instance is of a type that inherits from tclassb. So the "is" operator is translated into a call to the following helper routine

     function fpc_do_is(aclass : tclass;aobject : tobject) :
boolean;[public,alia
s: 'FPC_DO_IS']; {$ifdef hascompilerproc} compilerproc; {$endif}
       begin
          fpc_do_is:=assigned(aobject) and assigned(aclass) and
            aobject.inheritsfrom(aclass);
       end;

Jonas

Reply by email

Anton Tichawa
to Olle Raab: Is it possible ? In details: Suppose: program Test; type TClassA = class; TClassB =…

Hello, Olle!

One possibility is:

type
  TClassA = class(TObject);

Your classes then inherit all methods of TObject, including TObject.ClassType and TObject.InheritsFrom. So it's possible to write things like:

procedure P(x: TClassA);
begin
  if x.ClassType = TClassA then begin
    bla bla
  end else if x.ClassType = TClassB then begin
    bla bla
  end;
  if x.InheritsFrom(TClassC) then begin
    // true for TClassC and descendants
    bla bla
  end;
end;

hth,

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email

Michael Van Canneyt
to Olle Raab: Is it possible ? In details: Suppose: program Test; type TClassA = class; TClassB =…
Olle Raab wrote 29 lines
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.

Check the function
x.classname

It returns the class name.

Michael.

Reply by email

Anton Tichawa
to Marco van de Voort: Do you want to see it in the debugger? Then you need to try to examine tobject properties…

Hello, Marco!

if x IS TClassB then

What object code is generated for this? I assume that, informally speaking,

Ord(TClassB) = Ord(@VMT)

and that

if x IS TClassB

yields somthing like

if @(x.VMT) = TClassB?

Am I wrong here?

Thanks,

Anton.

----------

"Adas Methode war, wie sich zeigen wird, Tagträume in offenbar korrekte Berechnungen einzuweben."

Doris Langley Moore: Ada, Countess of Lovelace (London 1977).

----------

Anton Tichawa
Volkertstrasse 19 / 20
A-1020 Wien
mobil: +43 664 52 07 907
email:

----------

Reply by email