Prof A Olowofoyeku (The African Chief) wrote:
On 2 Jun 2003 at 21:34, Frank Heckenbach wrote:
I recently came about the issue of overriding non-virtual methods by virtual ones. GPC currently allows this, but it may be slightly confusing. BP allows it as well, so we shouldn't forbid it. But should we warn? (Always or unless `--borland-pascal' was given?)
A warning except under '--borland-pascal' is fine.
OK (chief52*.pas). (Also no warning under `--delphi', or should it?)
Another thing - an object with virtual methods but no constructor causes confusion under BP - e.g.,
type a = object procedure p; end;
b = object (a) procedure p; virtual; end;
procedure a.p; begin writeln ('a'); end;
procedure b.p; begin writeln ('b'); end;
var foo : a; bar : b;
begin foo.p; bar.p; end.
If this example is compiled with BP then, only 'a' gets printed (BP gives no warning, but silently compiles what it considers to be a faulty program).
Compiled with FPC, you get these warnings: a.pas(6,7) Warning: Virtual methods are used without a constructor in "b" a.pas(30,2) Warning: Variable "bar" does not seem to be initialized
When you run the program, 'a' gets printed, and then a GPF.
Compiled with Delphi, you get no warning, and both 'a' and 'b' get printed. Ditto with GPC, even with '--borland-pascal'.
Add a constructor and a constructor call before the call to 'b.p' and everything is alright under BP and FPC.
So, GPC's current treatment of objects with virtual methods but no constructor seems (at least with the trivial examples) to be consistent with Delphi, but not with BP when using '--borland-pascal'.
For GPC, your observed behaviour is expected. With what I know about BP, it isn't -- and in fact, when I tested it with BP, it gave a runtime error 210 (range checking or invalid object reference), and with `{$R-}', it hung. I don't know so much about Delphi and FPC, so I can't tell whether it's the expected behaviour there. Maybe it isn't guaranteed to work in Delphi and just happened to in your test (just like your BP test happened to not crash). Do you find anything in the documentation?
In all my Pascal code, I found just one place (and some GPC test programs) where objects have virtual methods and no constructor. These can be fixed easily.
So I think a warning cannot hurt, and unless someone protests, I'll add it (not for abstract object types, of course). (chief51*.pas)
Frank