[fpc-pascal] Create class descendant at runtime?

Martin Frb lazarus at mfriebe.de
Tue Apr 29 14:06:53 CEST 2014


On 29/04/2014 02:58, Craig Peterson wrote:
> Hi guys,
>
> Does Free Pascal or any of the included packages have a way to create a
> descendant of a class at runtime?  Before anyone tells me it's a bad
> idea, I know.  I have an existing class-based registration scheme that
> I'm adding some dynamically loaded plugins to and the alternatives are
> all messier.
>

Well. Nothing is impossible. But somethings are not advice-able.

It can be done. But it must be checked with every Version of FPC, it may 
break on every new version.
Same it must be checked with ever compiler setting, and may break with 
any of them.

Also: inherited calls will not work in such cases, and you need to write 
very messy code, in order for it to work with such a self build class.

The basic is:

PPointer = ^Pointer

PPointer(SomeObject)^  contains the address of the class data (size and 
content can be found in the RTL).

You can copy and modify that. You also must copy the VMT, and that means 
you need to know how many virtual methods you declared (there is no 
count stored / so you must know). And you must disable any optimizations 
to class structure/ vmt optimizations.

All in all, you will have really hard to read and easy to break code.





More information about the fpc-pascal mailing list