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: ------------------------------ program Foo;
type a = object procedure p; end;
b = object (a) private procedure p; { ??? } end;
procedure a.p; begin writeln('a.p'); end;
procedure b.p; begin a.p; writeln('b.p'); end;
var va:a; vb:b;
begin va.p; vb.p; end. ------------------------
the answer (you can follow step by step) is
a.p a.p b.p
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.
In fact the behaviour is also the same with virtual, but you need to add constructors and call them, otherwise it compiles but you obtain a bad crash.
Now I just tried with gpc, with or without virtual, the behavior is exactly the same.
The only difference is that private is not taken into account by gpc now (I no more obtain a warning ? ), and this was probably your problem. Your original question was: is it wise to allow overriding private/public methods ? In such a case both methods can be called from inside the objects with prefixing, and only the public one can be called directly from the outside world. For me this seems only a source of confusion, but may be someone has an idea for an useful application ?
Maurice