[fpc-devel] Visibility of overloaded ancestor method from a child class

Michał Gawrycki michal.gawrycki at gmsystems.pl
Fri Feb 7 20:27:48 CET 2025


 From documentation:

If the overload keyword is not present, then all overloaded versions 
must reside in the same unit, and if it concerns methods part of a 
class, they must be in the same class, i. e. the compiler will not look 
for overloaded methods in parent classes if the overload keyword was not 
specified.

https://www.freepascal.org/docs-html/ref/refse96.html

Regards, Michał.

W dniu 2025-02-07 o 14:23, Maxim Ganetsky via fpc-devel pisze:
> Hello.
> 
> The following program does not compile, because overloaded AddItem(s: 
> string) method in ancestor is not visible from child class. Is it normal?
> 
> program Test;
> {$MODE OBJFPC}
> type
>    TBase = class
>    public
>      procedure AddItem(s: string; i: integer); virtual;
>      procedure AddItem(s: string);
>    end;
>    TDesc = class(TBase)
>    public
>      procedure AddItem(s: string; i: integer); override;
>    end;
> 
> procedure TDesc.AddItem(s: string; i: integer);
> begin
>    writeln('TDesc(s,a) ', s, ' ', i);
> end;
> procedure TBase.AddItem(s: string; i: integer);
> begin
>    writeln('TBase(s,a) ', s, ' ', i);
> end;
> procedure TBase.AddItem(s: string);
> begin
>    AddItem(s, 123);
> end;
> 
> var
>    lBase: TBase;
>    lDesc: TDesc;
> begin
>    lBase := TBase.Create;
>    lDesc := TDesc.Create;
>    lBase.AddItem('test'); // OK
>    lDesc.AddItem('test'); // Error: Wrong number of parameters specified 
> for call to "AddItem"
>    readln;
> end.
> 

-- 


GM Systems
ul. K.Marksa 9
58-260 Bielawa
tel. +48 694 178 276
http://www.gmsystems.pl/



More information about the fpc-devel mailing list