Quote
Matthew wrote:
> In other words, why can't we send a TBitBtn as a TObject and expect
> Delphi to understand that the TObject is in fact a TBitBtn when it
> arrives as a parameter - simply by virtue of the fact that it is what
> it is.
> Opinion: Conceptually, it should be possible for Delphi to know
> exactly what type of TObject it is passing down at run time even if it
> is not specified in the code. It seems Borland just haven't done it.
Run-time, no problem. The problem is actually at compile time.
When code is translated, the symbolic names you and I use are
translated into offsets from some base. To the compiler, a
class is just a record. Each attribute has it's own offset.
The variable 'Self' is just a pointer to this record (and hence
the 'base' mentioned above). In addition to the attributes you see,
there are a couple of 'hidden' ones, among others a pointer to a
table where all pointers to virtual functions are stored. This
pointer identifies the class of the object.) So, when you use
TLabel.Caption, the compiler figures out this is at address
[Self + 16] (for instance). However, if you use TForm.Caption,
the address might be [Self + 32] (for instance). So you'll have
to tell it what object to calculate reference for.
This is the prize of fast compiled code. It's fast, but you
have to tell it what you want, so it's not as dynamic. If you
want, you can use the RTTI functions to find published
properties by name at run-time. This is (not) described in the
TypInf.PAS file (or TypInf.INT if you're on Delphi 1).
If you get the chance, I highly recomend taking a compiler
construction class or reading a compiler construction book. It's
hard work, but you'll understand much more about the magic that
goes on when you hit F9.
M.
--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no