Forcing the linker to include your class

To force the linker to include your class into executable (often needed for RTTI) you can just write single line in the unit initialization to do that and there is no need for dummy functions or any external code to “use” the class.

unit uTest;

interface

type
  [...]
  TTest = class(...)
    ...
  end;

implementation

...

begin
  TTest.ClassName; // That's all you need
end.

And you really do not need to define in each such unit a dumb empty procedure with a parameter and then call it as it’s done in some other sources. And do not say that calling ClassName on a class takes a lot of time and it’s not optimal! It takes only 63 ms for 1’000’000 calls and it will be never bottleneck of your app. Trust me. You will never have million classes… even close to that! If you have then it’s not a problem of ClassName call – it’s probably an architecture problem…

By the way: “TTest.ClassInfo;” will not work as it’s inline.

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s