开发者

Delphi 6: Force compiler error on missing abstract class methods?

开发者 https://www.devze.com 2023-01-04 15:11 出处:网络
I\'m using Delphi Pro 6.Right now, the only way to know if a c开发者_JAVA技巧lass is missing a base class abstract method is to wait for the IDE to emit a \"constructing instance of {derived class} co

I'm using Delphi Pro 6. Right now, the only way to know if a c开发者_JAVA技巧lass 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.


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);
  TAbstractClass1.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.


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".


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消