In Delphi, can I call an instance method of a class method with the same name?

Is it possible that in Delphi the class method calls the inherited instance method with the same name? For example, I tried something like this:

//... Skipped surrounding class definitions function TSomeAbstractDialogForm.Execute: Boolean; begin Result := ShowModal = mrOk; end; 

I had several specialized dialog classes that inherited the abstract form of dialogue, and each class had its own factory method:

 class function TSomeInheritingDialogForm.Execute: Boolean; var Fm: TSomeInheritingDialogForm; begin Fm := TSomeInheritingDialogForm.Create(nil); try Result := Fm.Execute; finally Fm.Free; end end; 

This approach led to an endless loop, because F.Execute, instead of calling the method of the alleged instance of the base class, continued to call the factory method again and again (the result is a bunch of created forms).

Of course, the obvious solution was to change the name of the factory method (I called it CreateAndShow), but I was curious. Why didn't the compiler warn me about a hidden method? Is there a way to explicitly call the instance method in this situation?
+4
source share
3 answers

You can try a hard throw. But it is better to rename the class function. (For example, CreateAndExecute).

Executing in the child class hides the execution in the parent class (I think the compiler will give a warning for this). You can access this with hard casting. But there is no way to distinguish between an instance method and a class method.

 function TSomeAbstractDialogForm.Execute: Boolean; begin Result := ShowModal = mrOk; end; class function TSomeInheritingDialogForm.Execute: Boolean; var Fm: TSomeInheritingDialogForm; begin Fm := TSomeInheritingDialogForm.Create(nil); try Result := TSomeAbstractDialogForm(Fm).Execute; finally Fm.Free; end end; 
+5
source

Result: = inherited Execute will not be executed, since it is called in the created variable, and not in the class method.

The problem is that it is a bad idea to have a class function and a static method with the same name. The compiler sees them as two separate worlds that can be written next to each other.

+1
source

Will not be

 Result := inherited Execute; 

do the trick?

0
source

All Articles