Board index » delphi » A question about some reserved words in OOP

A question about some reserved words in OOP

Could someone explain the use of the reserved words Virtual
and Constructor.  I know their used when creating a procedure
or function that may change in the object hierarchy but I'm
not quite sure how exactly it works.
Thanx
Steve
 

Re:A question about some reserved words in OOP


Quote
Steve Tanton wrote:

> Could someone explain the use of the reserved words Virtual
> and Constructor.  I know their used when creating a procedure
> or function that may change in the object hierarchy but I'm
> not quite sure how exactly it works.

VIRTUAL.

Consider (not quite 100%, see CONSTRUCTOR explanation)

  type
    a           = object
      procedure P;  virtual;
      procedure DoSomething;
      end;    { a }
    b           = object( a )
      procedure P;  virtual;
      end;    { b }

In A.DoSomething, procedure P is called.  If the object is actually of
type B, then B.P will be called.  If P were not virtual, A.P would be
called.  You can think of this as follows:  not virtual, then method
"visible" at compilation is called;  virtual, then a search is
performed, starting at the object's base class and going up the parent
class chain.

In reality, a search would not be inefficient, so instead, each
class has an associated VMT (Virtual Method Table) containing
pointers to the virtual methods of the class.  Each object of given
class has a pointer to the class's VMT.  Binding of method call at
run-time, as well as determining size of object and so on, involves
nothing more than indexing the VMT.

CONSTRUCTOR.
For BP, initializes the object's VMT pointer.  Before this, any call
to a virtual method will, likely as not, crash the program.  So any
class with a virtual method (inherited or directly defined) *must*
have a constructor (inherited or directly defined).

From an abstract type point of view, the constructor actually
constructs an object of the type:  a minimal object.  From a
programmers point of view, it performs first stage of initialization.

A BP constructor returns a boolean value (you can call it like a
boolean function).  Return value is TRUE by default, and FALSE if
the constructor returns by calling Fail.

DESTRUCTOR.
Clears up the mess.  Should be VIRTUAL to ensure destructor for base
class is executed when called through a pointer.  This includes calls
to the destructor from ancestor classes.

Hope this helps,

- Alf

Re:A question about some reserved words in OOP


Quote
Steve Tanton (stan...@isn.net) wrote:

: Could someone explain the use of the reserved words Virtual
: and Constructor.  I know their used when creating a procedure
: or function that may change in the object hierarchy but I'm
: not quite sure how exactly it works.

Read in the manual where they explain this. (I think the manuals is good.)
 There you get the basics. If it's still something you don't know/understand,
ask again. :-)

/Jonas
+------------------------------------------------------------------------+
| Who? Me?: Jonas Steverud                         | If All is One,      |
| EMail...: d4jo...@dtek.chalmers.se               |  is then One All?   |
| Home....: http://www.dtek.chalmers.se/%7Ed4jonas |                     |
+------------------------------------------------------------------------+

Other Threads