Home > Net >  How to call current level of a virtual method in Delphi?
How to call current level of a virtual method in Delphi?

Time:10-20

Let suppose two classes:

Parent = class
public
  procedure virtFunc(); virtual;
  procedure other();
end;

Child = class(Parent)
public
  procedure virtFunc(); override;
end;

Usually, calling virtFunc on a Child instance from anywhere will call the Child implementation of that method. However, sometimes it's useful to call the same level implementation:

procedure Parent.other();
begin
  virtFunc(); // I want to call Parent.virtFunc(), not Child.virtFunc()
end;

What did I tried?

Parent(Self).virtFunc()

(Self as Parent).virtFunc()

And obviously, I could (but this is not the question):

  • rename them differently (childFunc vs parentFunc),
  • remove the virtual.

How to call the current level (non-polymorphic) version of a method in Delphi?

For those who know c , I would like some equivalent to Parent::virtFunc()

CodePudding user response:

I think that the only way to do this is to:

  1. Implement Parent.virtFunc by a call to a non-virtual method in Parent.
  2. When you want to call virtFunc in a non-polymorphic way you call that non-virtual method rather than calling virtFunc.
  • Related