Maurice Lombardi wrote:
Frank Heckenbach a écrit:
BP seems to allow overriding a public method with a private one (and vice versa). Does this seem reasonable?
What do Delphi and other compilers do?
At first sight, I'd rather forbid this in GPC (as usual, with an exception in `--borland-pascal', but perhaps a warning even there?).
program Foo;
type a = object procedure p; end;
b = object (a) private procedure p; { ??? } end;
procedure a.p; begin end;
procedure b.p; begin end;
begin end.
Oh, wait a second, it gets really strange ...
unit U;
interface
type a = object procedure p; end;
b = object (a) private procedure p; end;
implementation
procedure a.p; begin WriteLn ('a.p') end;
procedure b.p; begin WriteLn ('b.p') end;
var v: b;
begin v.p end.
program Foo;
uses U;
var v: b;
begin v.p end.
Under BP, this says:
b.p a.p
What does it mean actually? It means that `b' has *two* methods called `p'. Within the unit, one of them shadows the other, and outside of the unit, the first one is hidden (or non-existent because not exported), and the second one becomes visible. That's crazy, isn't it?
Yes. Try:
[...]
This has nothing to do with private. You obtain the same results when commenting out private. The procedures are overriden only with the keyword "virtual". Otherwise they coexist and you can call either with prefixing.
Yes, so that's something different. Please note that in my example both variables are of type `b', and no explicit parent method calling is done. It's exactly the same code in the program and the end of the unit (also semantically -- at least so it should --, since the type `b' which is used refers to the same type in the unit and the program).
So this isn't so much about inheritance, but about there being (apparently) different scopes for private and public methods in BP -- but not quite, since you still get an error if you define both in the same object type.
The only difference is that private is not taken into account by gpc now (I no more obtain a warning ? ),
`private' works on a unit/module/program scope (which is BP compatible, as you can check for yourself), that's why I made a unit in my test program. (It may be debatable if that's useful, but that's another issue -- it may be meant as a substitute for C++ `friend's or so ...)
Frank