views:

62

answers:

3

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.

+1  A: 

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.

Craig Stuntz
+1  A: 

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

Cosmin Prund
There is another way that this can happen - if using class references. e.g. if you have a list class that has a (virtual) `GetItemClass` function, and you call `NewItem := GetItemClass.Create;`, the compiler can't determine that you will be instantiating an abstract class.
Gerry
+2  A: 

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.

Gerry
Interesting approach, I think I'll add that to my standard practices.
dummzeuch
This is the result of tracking down run-time abstract errors, created when using ClassReference.Create, rather than TClassXXX.Create
Gerry
Excellent point Gerry. Those are a *real* pain to track down since the run-time error can seem so divorced from the real cause.
Robert Oschler

related questions