Board index » delphi » OOP con- & destructor

OOP con- & destructor

hiho !

i'm quite familiar with OOP using C++. When trying to model
some objects under BP7.0 I discovered that the declared
constructor and destructor of a borland pascal object is
never called automatically, so I'm asking myself what the
hell they are good for ?!
I could declare them as procedures instead of using the keywords
constructor etc. right ?

can anybody help me on this ?

regards, thomas
--

-------====:: Thomas Auinger ::: auin...@uni-paderborn.de ::====-------
"who is this general failure - and why $#&* is he reading my harddisc ?"

 

Re:OOP con- & destructor


Quote
Thomas Auinger (auin...@cadlab.de) wrote:

: i'm quite familiar with OOP using C++. When trying to model
: some objects under BP7.0 I discovered that the declared
: constructor and destructor of a borland pascal object is
: never called automatically, so I'm asking myself what the
: hell they are good for ?!

If I'm not totally wrong this is how it is:

If you use virtual methods you *got* to have de/constructors and call
them before you use the object. This is becourse of they are doing some
important initializations (sp?) (the virtual method table or whatever it's
called). The constructor makes some memory allocation and the destructor
does the deallocation. Therefor it's very wise to use them and call
them *before* you call any other of the methods. ;)
 Pascal does some allocation on it's own and therfor you don't *nead* them
always (i.e. when you don't use virtual methods).

: I could declare them as procedures instead of using the keywords
: constructor etc. right ?

100% correct if you don't use virtual methods.

BTW: In C++ the names of the constructor/destructors are defined by the
language but Pascal admitts you to make the names on your own.
 When I read about C++ last summer I got the fealing that Borland
took the best out of C++ and made the other stuff better.
 IMHO BP is better then C++ when you see to the planning and implementation
of the ideas about OOP. But that's my opinion... and doesn't need to have
anything to do with the reality. :-)

I hope I got the question right and this helps you.
But RTFM too. I think they explains it rather well there. :-)

/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 |                     |
+------------------------------------------------------------------------+
==========================================================================
Microsoft Network is prohibited from redistributing this work in any
form, in whole or in part without license. License to distribute this
work  is  available  to  Microsoft at $500.  Transmission without
permission constitutes an agreement to these terms.
==========================================================================

Re:OOP con- & destructor


Quote
On Fri, 1 Dec 1995, Thomas Auinger wrote:
> hiho !

> i'm quite familiar with OOP using C++. When trying to model
> some objects under BP7.0 I discovered that the declared
> constructor and destructor of a borland pascal object is
> never called automatically, so I'm asking myself what the
> hell they are good for ?!
> I could declare them as procedures instead of using the keywords
> constructor etc. right ?

> can anybody help me on this ?

> regards, thomas

It is interesting to see this question (or a variation) come up about
every three or four days...  Since I haven't answered a const/dest
question yet, I guess it is my turn...

Virtual methods are magical things that let the compiler call the right
procedure just by looking at [hidden] data stored with each object.  This
data is initialized by any procedure declared as a constructor, and the
memory allocated for this data is freed when you call the destructor.  So
you need to call a constructor before you call any virtual methods (if
you don't, your computer will hang REALLY HARD).  If your object has no
virtual methods, there is no reason to have a constructor or destructor,
but it is a good style convention just in case you later put in a virtual
method.

Re:OOP con- & destructor


Excerpts from netnews.comp.lang.pascal.borland: 4-Dec-95 Re: OOP con- &
destructor by Joe Willcox...@wcom.com

Quote
> Thomas Auinger <auin...@cadlab.de> wrote:
> >i'm quite familiar with OOP using C++. When trying to model
> >some objects under BP7.0 I discovered that the declared
> >constructor and destructor of a borland pascal object is
> >never called automatically, so I'm asking myself what the
> >hell they are good for ?!
> >I could declare them as procedures instead of using the keywords
> >constructor etc. right ?

> Sort of.

> You can supply constructors as an argument to "new":

> pSomeObject := new(PSomeObject, init(arg1, arg2));

> You can supply a destructor as an argument to dispose:

> dispose(pSomeObject, done);

Also, by using the word constuctor or destructor in your declaration,
you tell pascal that you might use virual methods in your object.  If
you don't have constuctors and desctructors, you can't use virtual
methods.  And if you're not going to use virtual methods or pointers to
refer to your objects, you can use procedure init; and procedure done;
instead.

--- Kyle Oppenheim  kyl...@andrew.cmu.edu
         Home Page: http://www.contrib.andrew.cmu.edu/~kyleo/kyle.html
Illusion Home Page: http://www.contrib.andrew.cmu.edu/~kyleo/illusion.html

Re:OOP con- & destructor


Quote
d4jo...@dtek.chalmers.se (Jonas Steverud) wrote:
>BTW: In C++ the names of the constructor/destructors are defined by the
>language but Pascal admitts you to make the names on your own.
> When I read about C++ last summer I got the fealing that Borland
>took the best out of C++ and made the other stuff better.
> IMHO BP is better then C++ when you see to the planning and implementation
>of the ideas about OOP. But that's my opinion... and doesn't need to have
>anything to do with the reality. :-)

You are right...  It doesn't have anything to do with reality.  The
constructor implementation in C++ is much better than BP.  In C++, when
you declare an instance of an object type, that object is constructed
when it is declared.  It does not need a separate call to a constructor.
 That is a much better implementation and is more in the C++ design goal
of treating objects the same as a native type.

Also, when a object is disposed of, its destructor also gets called
automatically.  That, too, is a much better design.

--
How come 6 of the 9 speakers at Xhibition '94 were from Microsoft?
joe.willcox...@wcom.com
(918)588-3673
http://wrc.wcom.com/IS/ISOrg/HomePages/ITStrategies/JWillcoxson

Re:OOP con- & destructor


Quote
Thomas Auinger <auin...@cadlab.de> wrote:
>i'm quite familiar with OOP using C++. When trying to model
>some objects under BP7.0 I discovered that the declared
>constructor and destructor of a borland pascal object is
>never called automatically, so I'm asking myself what the
>hell they are good for ?!
>I could declare them as procedures instead of using the keywords
>constructor etc. right ?

Sort of.

You can supply constructors as an argument to "new":

pSomeObject := new(PSomeObject, init(arg1, arg2));

You can supply a destructor as an argument to dispose:

dispose(pSomeObject, done);

The constructors/destructors are not much more than glorified procedures in BP, whereas in
C++ they are much better done.  IHNSHO, I think the way constructors and destructors are
implemented are one of the most important features of the language.  I do not know where
the vtable pointer gets set in BP, but it is most likely to be set after the constructor
call.  That is another important reason for having constructors.

In Delphi, all of the new objects are really references to objects on the heap.  The
Create/Destroy cycle of those objects are very dependent on the constructor/destructor
procedure being called.
--
How come 6 of the 9 speakers at Xhibition '94 were from Microsoft?
joe.willcox...@wcom.com
(918)588-3673
http://wrc.wcom.com/IS/ISOrg/HomePages/ITStrategies/JWillcoxson

Other Threads