[fpc-pascal]using 'overload'

Peter Vreman peter at freepascal.org
Sun Apr 6 14:29:21 CEST 2003

At 14:09 6-4-2003, you wrote:
>Is it necessary to use the 'overload' directive in objfpc mode?
>E.g. this code doesn't work:
>{$MODE objfpc}
>Program Test2;
>  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);
>  DoStuff(a, 0);
>Procedure TFunClass.DoStuff(a, b : Integer);
>  Writeln('TFunClass.DoStuff(', a, ',', b, ')');
>  q : TFunClass;
>  q := TFunClass.Create;
>  q.DoStuff(5);
>  q.Destroy;
>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


More information about the fpc-pascal mailing list