Class helpers and virtual methods – E2003 Undeclared identifier: ‘QueryInterface’/ ‘_AddRef’/’_Release’
Posted by jpluimers on 2018/06/20
Reminder to self: if you want to stay compatible with old (Pre Delphi XE) source code, then do not use virtual methods in class helpers.
Technically you can, as class helpers can inherit from other class helpers, but this is only supported as of Delphi XE.
In lower versions you get these errors as apparently the Delphi compiler tags an IInterface to the class helper compiler result:
[Error] Project1.dpr(14): E2003 Undeclared identifier: 'QueryInterface'
[Error] Project1.dpr(14): E2003 Undeclared identifier: '_AddRef'
[Error] Project1.dpr(14): E2003 Undeclared identifier: '_Release'
Source: [WayBack] QualityCentral 9782
–jeroen
rvelthuis said
Currently, a virtual call in a class helper is implemented through an interface (and has quite some overhead, as far as I can tell). I guess that was the intention in XE too, but it was not properly implemented.
Due to the considerable overhead, even these days (Tokyo), it is not a good idea to add a virtual method through a class helper. It works, but tends to be slow. After all, the VMT of the class can’t be extended, so GetInterface must be called and the method is called on the interface.
jpluimers said
Thanks for that. Boy why did I not think of that?