This doesn't compile (redefinition of CODE), likewise using class vars doesn't work for the same reason, and if you put just one class var in the base (TA) type, it will be common to all descendant types.You are wrong, here....:
Simply deref'ing some offset from the VMT is much faster than a function call.Then declare the function inline. That does exactly that!
[…] assign a unique ID […] to a set of classes all of which inherit from some base type. […]And typeInfo? typeInfo (https://www.freepascal.org/docs-html/ref/refse19.html) is a compiler function and returns a PTypeInfo (https://www.freepascal.org/docs-html/rtl/typinfo/ptypeinfo.html). So, it basically results in a mov-instruction, thus virtually has the same, if not less, overhead than introducing and maintaining an extra symbol.
type
TFooClass = class of TFooA;
TFooA = class
class const
FOO: string = 'something';
end;
TFooB = class(TFooA)
class const
FOO: string = 'something new'; // implicit override
end;
var
fc: TFooClass;
begin
fc := TFooA;
WriteLn(fc.FOO); // print something
fc := TFooB;
WriteLn(fc.FOO); // print something new
end.
What you want is my first example. Copy it exactly. That code is bug free. The mode does not matter as lomng as it is Delphi or ObjFpc, my code also compiles in {$mode objfpc} without any error hint or warning.
Are you using a modern FPC 3.0.4? If not, upgrade.
Note you can not assign a class of type TB to a class reference of type TA. That's programmer error. Of course it prints zero: it becomes a reference to TA, not TB.
And you don need it. What the compiler told you is true:"Error: Incompatible types: got "Class Of TB" expected "TA" Both need to be class references: with a TBClass the code compiles, but it will still print zero, because you ask for TA, NOT TB. Again, do not use class references here....