On 20 Mar 2001, at 16:23, Frank Heckenbach wrote:
[...]
The interesting case, of course, is when baz is an ancestor of bar, and this requires a runtime check.
This little program shows how Delphi would handle it;
uses classes;
type f1 = class (TStringList) Procedure foo; virtual; end; type f2 = class (f1) Procedure bar; virtual; end;
procedure f1.foo; begin end; procedure f2.bar; begin end;
var h1 : f1; h2 : f2; begin h1 := f1.create; h2 := f2.create;
{--- these all return TRUE ---}
And should all give a warning (in GPC) because it's clear at compile time that they're true, and therefore the checks are either superfluous or not what the programmer means...
The example is just a trivial one. In Delphi, event handlers normally take a "Sender : TObject" parameter which normally points to the control that generated the event. Inside the event handler (method) you may need to know the exact type of the "Sender". It is in such cases that the "if Sender is TEdit" scenario will be necessary (almost everything is a descendant of TObject and TObject itself doesn't do very much).
{--- this returns FALSE ---} Writeln (h1 is f2);
That's the interesting case because h1 could (at runtime) be an f2.
If you "Constructed" it earlier with "h1 := f2.create;" instead of "h1 := f1.create;" then "h1 is f2" will also be TRUE. Also, if somewhere down the line you do "h1 := h2;" then "h1 is f2" will also be TRUE (but you will get an exception when you come to free h1 unless you have overridden the destructor to cater for situations where "h1 is f2".
Interestingly, the compiler does not accept the assignment: "h2 := h1;"
Best regards, The Chief --------- Prof. Abimbola Olowofoyeku (The African Chief) Author of Chief's Installer Pro v5.25 for Win32 Email: African_Chief@bigfoot.com http://www.bigfoot.com/~african_chief/