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.
Yes, I am using FPC 3.0.4 (svn r56594). It does not compile, I promise
. I took a New Project -> Program, pasted your code in after the "uses", and got the following errors:
Free Pascal Compiler version 3.0.4 [2017/12/03] for i386
...
project1.lpr(21,7) Error: (5002) Duplicate identifier "CODE"
project1.lpr(21,7) Hint: (5003) Identifier already defined in unit PROJECT1 at line 15
project1.lpr(27,7) Error: (5002) Duplicate identifier "CODE"
project1.lpr(27,7) Hint: (5003) Identifier already defined in unit PROJECT1 at line 15
...
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....
Sorry but I have no clue what you're going on about here. "class of TA" is a class reference / metaclass (implemented as a VMT pointer). Something of this type can be assigned any VMT pointer that is either TA itself or inherits from TA, and can be used to dynamically create classes of different types, as per
https://www.freepascal.org/docs-html/ref/refse34.html. Here TB inherits from TA, so it's fine to assign ac := TB.
I do need this functionality in order to pass the list of class definitions into the framework and then have the framework spit out objects of a requested type, as in the doc example. It appears this is just broken or behaves completely differently in {$mode delphi}. Perhaps the misunderstanding here is simply that you're doing everything in {$mode delphi} and I'm using {$mode objfpc} ?
@hnb: sounds promising, I look forward to the new function. I considered hacking the VMT in similar ways to the link you provided, but it seems really ugly, particularly as you first have to first convince the OS to let you edit it. (How would you even free the GetMem'd memory off in the example given there? I guess you'd have to register it in some store and iterate through the allocated blocks in the finalization section.)
ahydra