[fpc-pascal]Bug with virtual methods

Johann Glaser Johann.Glaser at gmx.at
Tue Dec 31 11:35:42 CET 2002


Hi!

Please have a look at the attached program. There are two classes, the
second one inherits all methods from the first one.

The method M1 with the string parameter is the "acting" function. That
one with the Integer parameter is just a wrapper to handle integers.

In the second class the string method is overridden. Therefore it was
defined virtual in C1. C1.M1(I:Integer), which is inherited to C2,
always calls the string method which belongs to the class.

The main program demonstrates this. It creates two instances, one of C1,
one of C2. Then C1.M1(String) is executet, its wrapper C1.M1(Integer).
Both work properly.

Then C2.M1(String) is executed. Finally C2.M1(Integer) is executed. Here
a compiler error occurs. The compiler has forgotten to inherit the
C1.M1(Integer) to C2!!!

If C1.M1(Integer) is renamed to C1.M2(Integer) no problem occurs. It
seems, the compiler only inherits _one_ of parameter-overloaded methods.

Bye
  Hansi

-- 
Johann Glaser   <Johann.Glaser at gmx.at>
   Vienna University of Technology
       Electrical Engineering 
____ http://www.johann-glaser.at/ ____
-------------- next part --------------
{$MODE OBJFPC}
Program Bug;

Type  C1 = class
             Procedure M1(St:String); virtual;
             Procedure M1(I:Integer);
           End;
      C2 = class(C1)
             Procedure M1(St:String); override;
           End;

Procedure C1.M1(St:String);
Begin
  WriteLn(St);
End;
Procedure C1.M1(I:Integer);
Var St : String;
Begin
  Str(I,St);
  M1(St);
End;
Procedure C2.M1(St:String);
Begin
  WriteLn('+',St,'+');
End;

Var I1 : C1;
    I2 : C2;
    St : String;
    I  : Integer;
    
Begin
  St := 'abc';
  I  := 123;
  I1 := C1.Create;
  I2 := C2.Create;
  I1.M1(St);
  I1.M1(I);
  I2.M1(St);
  I2.M1(I);   { <-- compiler error here: "Incompatible type for arg no. 1: Got SHORTSTRING, expected PCHAR" }
End.



More information about the fpc-pascal mailing list