Board index » delphi » TPW1.5 - constructors and destructors

TPW1.5 - constructors and destructors

I have just written an object to look after one of my data
files and in doing so I wondered why it needs constructors
and destructors instead of just procedures.

I have RTM but all it said was how to do it, not why.
Can anyone shed some light on this. Also why does the
manual say you can have many constructors and destructors,
for what purpose.
http://users.powernet.co.uk/joppa/index.html
mailto:kevin.j...@joppa.powernet.co.uk

 

Re:TPW1.5 - constructors and destructors


Kevin Jory <kevin.j...@joppa.powernet.co.uk> wrote in article
<3363c56d.30073...@news.powernet.co.uk>...

Quote
> I have just written an object to look after one of my data
> files and in doing so I wondered why it needs constructors
> and destructors instead of just procedures.
> I have RTM but all it said was how to do it, not why.

When you use dynamic methods (that is also known as "late-binding") as
opposed to static you will recall from the manual that the address of the
procedure/function is determined at run-time.  Ah, but when at run-time?
The constructor is responsible for initialising a field of the object
(hidden from the programmer, but an object field nontheless) which is a
pointer to that object classes Virtual Method Table.  This in tern defines
the addresses of each individual method within that object.  Phew!

A consequence of this is that if you don't declare your methods 'virtual'
you don't need a constructor/destructor.  However, if you inherit from an
object with virtual methods, you must use the constructor (either inherited
or redefine your own)

Quote
> Can anyone shed some light on this. Also why does the
> manual say you can have many constructors and destructors,

You can have many constructors, and this can be useful.  For example,
suppose you have a TWidget object.  
You might want to initialise the object directly, by using
new(MyWidget,Init);  
Or you might want to load it from disk, new(MyWidget,Load(filename));
Or perhaps copy it from another widget, new(MyWidget,Copy(AnotherWidget));

Note that these different constructors don't neccessarily require different
destructors.  Just remember that you can only put constructor calls in the
New statement, and destructors in the Dispose statement.

Re:TPW1.5 - constructors and destructors


In article: <01bc5410$83719440$b7139...@hal9k.demon.co.uk>  "David Jones"

Quote
<news-p...@hal9k.demon.co.uk> writes:

>Note that these different constructors don't neccessarily require different
>destructors.  Just remember that you can only put constructor calls in the
>New statement, and destructors in the Dispose statement.

Not true. This would compile and run quite happily:

var
  Widget: TWidget;

begin
  Widget.Init;
  Widget.DoSomething;
  Widget.Done;
end;  

-- Jay

 --------------------------------------------------------------------------
| Jason Burgon - author of Graphic Vision, TV-Like GUI for 256 Colour SVGA |
| g...@jayman.demon.co.uk   ftp://SimTel/msdos/turbopas/gv4svga1.zip         |
| ***NEW VERSION OF GV AVAILABLE FROM*** -> http://www.jayman.demon.co.uk  |
 --------------------------------------------------------------------------

Re:TPW1.5 - constructors and destructors


Quote
"David Jones" <news-p...@hal9k.demon.co.uk> wrote:

:Kevin Jory <kevin.j...@joppa.powernet.co.uk> wrote in article
:<3363c56d.30073...@news.powernet.co.uk>...
:> I wondered why it needs constructors and destructors
:> instead of just procedures.

:When you use dynamic methods <snip explanation>.

:A consequence of this is that if you don't declare your methods 'virtual'
:you don't need a constructor/destructor.

Looks like I'm safe. It's a new object, no decendancy,
no virtual, just a design on its own. Still, I'll call
them constructor and destructor for convention but not
worry about any special code to use them just, as was
said, use New and Dispose to activate them.

FYI the constructor opens a data file and the
destructor closes it.

Thanks for the explanation.
http://users.powernet.co.uk/joppa/index.html
mailto:kevin.j...@joppa.powernet.co.uk

Other Threads