Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2025-02

Visibility of overloaded ancestor method from a child class

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Maxim Ganetsky

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.
Best regards, Maxim Ganetsky

Reply by email

Bart
to Maxim Ganetsky

On Fri, Feb 7, 2025 at 2:40 PM Maxim Ganetsky via fpc-devel wrote:

It compiles if you declare the methods in TBase as "overload". D7 refuses to compile this code (even with "overload"), but D7 is a little bit old...

Bart

Reply by email

Bart
to Bart
D7 refuses to compile this code (even with "overload"), but D7 is a little bit old...

Well, Delphi is a bit picky about the order: it must be "overload; virual;" (I had "virual; overload;")
So, with that fixed, D7 compiles the code as well.

Bart

Reply by email

Michał Gawrycki
to Maxim Ganetsky: Hello. The following program does not compile, because overloaded AddItem(s: string)…

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:

Show quoted text 43 lines
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/

Reply by email

Maxim Ganetsky
to Michał Gawrycki

07.02.2025 22:27, Michał Gawrycki пишет:

Show quoted text 9 lines
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

Thanks, guys.

Show quoted text 48 lines
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. >
Best regards, Maxim Ganetsky

Reply by email