[fpc-pascal] The "overload" keyword

Sven Barth pascaldragon at googlemail.com
Wed Nov 13 09:32:35 CET 2013


Am 13.11.2013 03:35, schrieb Xiangrong Fang:
> Hi All,
>
> According to http://www.freepascal.org/docs-html/ref/refsu72.html,
>
> the overload keyword is mostly optional and for delphi compatiblity 
> only. But on Oct. 27, Sven replied my question about hide a public 
> constructor in base class:
>
> >>  I don't understand why this does not work:
>
> >> f := TFileStream.Create;
> >>
> >> I know that TFileStream's Create require a file name parameter, but 
> as TFileStream is
> >> inherited from TObject, which has a Create without parameter.  This 
> is why I feel that it
> >> is possible to HIDE a public constructor from ancestor.
>
> > No, I don't think so. If you want users to use the original 
> constructor as well you'd need to
> > declare yours as "overload". If it would be possible to callTFileStream.Create without
> > parameters then the instance would be in an inconsistent state, so only calling the
> > constructor with arguments should be possible (which is the case forTFileStream.
>
> > So this is definitely by design.
>
> Now my question is: in this case overload keyword DO make a 
> difference? Then, how to overload? e.g.:
>
> In base class:
>
> constructor Create;
>
> In child class:
>
> constructor Create(param1:...; param2: ...): overload;
>
> i.e. overload does NOT require functions to have same parameter list 
> or return type? Also, it does not require the overload keyword in base 
> class? Otherwise programmers need to modify the base class, which is 
> not good.
>
Take a look at this example:

=== code begin ===

program toverload;

{$mode objfpc}

type
   TTest = class
     constructor Create(aArg1: Integer);
   end;

constructor TTest.Create(aArg1: Integer);
begin

end;

var
   t: TTest;
begin
   t := TTest.Create;

end.

=== code end ===

This will fail to compile, because TTest.Create will hide the default 
constructor of TObject (which takes no arguments). If you now add 
"overload" the code will compile. So you can control whether inherited 
methods with different arguments (return type does not count!) is 
visible in your derived class. If the method in the base and the child 
have the same argument list than the one in the child will always hide 
the one in the base.
You don't need to add "overload" in the base class (here TObject). The 
same works for global functions/procedures in units as well btw.

The other use of overload is the one where Delphi requires "overload" 
and FPC allows it optionally (except in mode Delphi where it is 
required): to declare to functions/procedures in the same unit or two 
methods in the same class with different argument lists.

I hope this explains :)

Regards,
Sven
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20131113/c9f1aaae/attachment.html>


More information about the fpc-pascal mailing list