Alex Rivera | Logout

Reintroducing functions in Delphi

Asked 2008-09-10T11:39:50.467
38

What was the motivation for having the reintroduce keyword in the Delphi Programming Language?

If you have a child class that contains a function with the same name as a virtual function in the parent class and it is not declared with the override modifier then it is a compile error. Adding the reintroduce modifier in such situations fixes the error, but I have never grasped the reasoning for the compile error.

Edit
Report

1 Answer

2

Reintroduce tells the compiler you want to call the code defined in this method as an entry point for this class and its descendants, regardless of other methods with the same name in the ancestors’ chain.

Creating a TDescendant.MyMethod would create a potential confusion for the TDescendants in adding another method with the same name, which the compiler warns you about.
Reintroduce disambiguates that and tells the compiler you know which one to use.
ADescendant.MyMethod calls the TDescendant one, (ADescendant as TAncestor).MyMethod calls the TAncestor one. Always! No confusion…. Compiler happy!

This is true whether you want the descendant method to be virtual or not: in both cases you want to break the natural linkage of the virtual chain. And it does not prevent you from calling the inherited code from within the new method.

  1. TDescendant.MyMethod is virtual: ...but you cannot or don’t want to use the linkage.
    • You cannot because the method signature is different. You have no other choice as overriding is impossible in this case with return type or parameters not exactly the same.
    • You want to restart an inheritance tree from this class.
  2. TDescendant.MyMethod is not virtual: You turn MyMethod into a static one at the TDescendant level and prevent further overriding. All classes inheriting from TDescendant will use the TDescendant implementation.
answered 2008-09-16T00:13:43.607

Your Answer