Sign in Join

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

Mailing lists / fpc-pascal / 2003-04

using 'overload'

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Nikolay Nikolov

Is it necessary to use the 'overload' directive in objfpc mode?

E.g. this code doesn't work:

{$MODE objfpc}
Program Test2;
Type
  TBaseFunClass = Class(TObject)
    Procedure DoStuff(a : Integer; b : Integer); Virtual; Abstract;
    Procedure DoStuff(a : Integer);
  End;
  TFunClass = Class(TBaseFunClass)
    Procedure DoStuff(a, b : Integer); Override;
  End;
Procedure TBaseFunClass.DoStuff(a : Integer);
Begin
  DoStuff(a, 0);
End;
Procedure TFunClass.DoStuff(a, b : Integer);
Begin
  Writeln('TFunClass.DoStuff(', a, ',', b, ')');
End;
Var
  q : TFunClass;
Begin
  q := TFunClass.Create;
  q.DoStuff(5);
  q.Destroy;
End.

fpc 1.1 says:
...
test2.pas(30,14) Error: Wrong number of parameters specified test2.pas(10,15) Hint: Found declaration: TFunClass.DoStuff(Longint,Longint) test2.pas(33) Fatal: There were 1 errors compiling module, stopping

fpc 1.0.6 gives the same error, only without the hint :-)

When I put 'overload;' everywhere it works. Is it necessary to use 'overload;' everywhere in objfpc mode or it's just a 'feature' of the compiler. And what's the reason why Borland introduced the 'overload' directive for function overloading?

Peter Vreman
to Nikolay Nikolov
you wrote 49 lines
Is it necessary to use the 'overload' directive in objfpc mode? E.g. this code doesn't work: {$MODE objfpc} Program Test2; Type TBaseFunClass = Class(TObject) Procedure DoStuff(a : Integer; b : Integer); Virtual; Abstract; Procedure DoStuff(a : Integer); End; TFunClass = Class(TBaseFunClass) Procedure DoStuff(a, b : Integer); Override; End; Procedure TBaseFunClass.DoStuff(a : Integer); Begin DoStuff(a, 0); End; Procedure TFunClass.DoStuff(a, b : Integer); Begin Writeln('TFunClass.DoStuff(', a, ',', b, ')'); End; Var q : TFunClass; Begin q := TFunClass.Create; q.DoStuff(5); q.Destroy; End. fpc 1.1 says: ... test2.pas(30,14) Error: Wrong number of parameters specified test2.pas(10,15) Hint: Found declaration: TFunClass.DoStuff(Longint,Longint) test2.pas(33) Fatal: There were 1 errors compiling module, stopping fpc 1.0.6 gives the same error, only without the hint :-) When I put 'overload;' everywhere it works. Is it necessary to use 'overload;' everywhere in objfpc mode or it's just a 'feature' of the compiler. And what's the reason why Borland introduced the 'overload' directive for function overloading?

You need to define overload here because you want to explicitly overload the functions defined in the parent class.

Without defining the overload keyword overloading will only be done in the current scope.
When defining the overload keyword overloading will be done across the scope, for classes and objects this will overload functions in parent classes. And for unit level the overloading will be done across all units.

The reason for this is to be compatible with the old overloading behaviour of FPC and also with Delphi which always requires the overload keyword and also does this cross scope overloading

Peter