Delphi 6: Force compiler error on missing abstract

2019-06-26 07:50发布

I'm using Delphi Pro 6. Right now, the only way to know if a class is missing a base class abstract method is to wait for the IDE to emit a "constructing instance of {derived class} containing abstract method {base class.abstract method name}" warning or to wait for a runtime Abstract Error method when an attempt to call the missing method is made. The former isn't sufficient since it only finds warnings for those derived classes actually constructed in the current project. The latter is just plain painful.

It would be so much better if Delphi output a fatal warning for all classes that do not declare/implement a base class abstract method immediately. Does anyone know a way to set this up or a plug-in that does this?

Thanks.

3条回答
【Aperson】
2楼-- · 2019-06-26 08:23

A class containing abstract methods is only dangerous if you instantiate the class, so Delphi's warning is spot-on. You only get the abstract-error run time exception if you ignored at least one "instantiating class with abstract methods".

查看更多
smile是对你的礼貌
3楼-- · 2019-06-26 08:30

I've found the simplest way to do this is to add a section in the unit initialization area using a conditional define that creates an instance of each class that you think shouldn't have any abstract methods:

{$IFDEF CheckAbstracts}
initialization
  TSubclass1.Create(params);
  TAbstactClass1.Create(params); // Gives constructing instance of {derived class} containing abstract method warning
{$ENDIF}

Compile with the CheckAbstracts conditional, and you will get warnings whenever you have an incompletely implemented class.

查看更多
戒情不戒烟
4楼-- · 2019-06-26 08:36

It's valid to not implement these methods. You might intend to implement the abstract method in yet another subtype.

A later version of Delphi/Win32 (I don't remember which) introduced formal abstract classes, which make it clear when you do and do not intend to instantiate the type. If you're rigorous about using this, the feature you request would then make sense. But for D6 it is not clear.

查看更多
登录 后发表回答