I have a lot of descendant of my class:
PMyAncestor =^TMyAncestor;
TMyAncestor = object
public
constructor init;
destructor done; virtual;
// There are virtual methods as well
end;
PMyDescendant1 =^TMyDescendant1;
TMyDescendant1 = object ( TMyAncestor )
end;
PMyDescendant2 =^TMyDescendant2;
TMyDescendant2 = object ( TMyAncestor )
end;
PMyDescendant3 =^TMyDescendant3;
TMyDescendant3 = object ( TMyDescendant2 )
end;
procedure foo;
var
pMA1, pMA2, pMA3, pMA4 : PMyAncestor;
s : string;
begin
pMA1 := new( PMyAncestor, init );
pMA2 := new( PMyDescendant1, init );
pMA3 := new( PMyDescendant2, init );
pMA4 := new( PMyDescendant3, init );
try
s := some_magic( pMA1 ); // s := "TMyAncestor"
s := some_magic( pMA2 ); // s := "TMyDescendant1"
s := some_magic( pMA3 ); // s := "TMyDescendant2"
s := some_magic( pMA4 ); // s := "TMyDescendant3"
finally
dispose( pMA4, done );
dispose( pMA3, done );
dispose( pMA2, done );
dispose( pMA1, done );
end;
end;
Is there any way to get the class name of its descendant instances? I don't want to create a virtual method for this reason (there are thousands of descendants).
I know there is the typeOf(T) operator. But what is its return type? OK. Pointer. But what can I cast it for? The cast to PTypeInfo seems to be wrong.