How to define a virtual function whose parameters differ in descendant classes?

I want to have a copy procedure that is defined as virtual in the base class and implemented later in the derived class. The problem is that it is not allowed to redefine the procedure due to different parameters. Is there any solution with a Delphi class model or is my approach completely wrong?

type TCar = class procedure CopyFrom(c: TCar); virtual; end; TChrysler = class(TCar) FColor: Integer; procedure CopyFrom(c: TChrysler); override; end; procedure TCar.CopyFrom(c: TCar); begin //virtual end; procedure TChrysler.CopyFrom(c: TCrysler); begin FColor := c.FColor; end; var Car1, Car2: TCar; begin Car1 := TChrysler.Create; Car2 := TChrysler.Create; Car2.CopyFrom(Car1); //TChrysler.CopyFrom should be called. end; 
+4
source share
1 answer

You need to keep the parameter list the same, just do a type check inside the implementation:

 type TCar = class procedure CopyFrom(c: TCar); virtual; end; TChrysler = class(TCar) FColor: Integer; procedure CopyFrom(c: TCar); override; end; procedure TCar.CopyFrom(c: TCar); begin //virtual end; procedure TChrysler.CopyFrom(c: TCar); begin if c is TCrysler then FColor := TCrysler(c).FColor; inherited; end; 
+5
source

All Articles