Sign in Join

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

Mailing lists / fpc-pascal / 2013-11

The "overload" keyword

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

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 call TFileStream.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 for

TFileStream.

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.

Thanks,

Xiangrong

Reply by email

Sven Barth
to Xiangrong Fang
Xiangrong Fang wrote 42 lines
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

Reply by email