Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using an Initialize-function or destroy/reallocate?

23 views
Skip to first unread message

JiiPee

unread,
Oct 18, 2017, 2:34:16 PM10/18/17
to
Been thinking this many times: Which one is normally better, have an
initilize/reset function or destroy the object and just create a new
object (which would then have automatically everything initialized to
first values by the constructor). When you have to "zero" the object to
its initial values (the same values as when the object is created).

Öö Tiib

unread,
Oct 18, 2017, 4:06:48 PM10/18/17
to
Normally it is better when objects are not reused in different roles.
When we want to reuse object (for example in two roles) then it may
make it dim and hard to follow (for rest of the program, for reader
of code, for person debugging it) in what role it is. Additionally we
must be sure that need for first role is always over by the time we
use object for second role. If the roles are unrelated then that
is lucky coincidence and may change and cause incorrect results.

Reusing objects may give performance advantage because of reduced
amount of allocations and deallocations. If that benefit is measurable
then managing a whole pool (recycling center) of such objects typically
gives even more notable advantage than reusing single objects here
or there.

JiiPee

unread,
Oct 19, 2017, 8:56:38 AM10/19/17
to
On 18/10/2017 21:06, Öö Tiib wrote:
> On Wednesday, 18 October 2017 21:34:16 UTC+3, JiiPee wrote:
>> Been thinking this many times: Which one is normally better, have an
>> initilize/reset function or destroy the object and just create a new
>> object (which would then have automatically everything initialized to
>> first values by the constructor). When you have to "zero" the object to
>> its initial values (the same values as when the object is created).
> Normally it is better when objects are not reused in different roles.

Thanks, interesting view... something to think about. Ok so maybe I
should delete the object and create new one.
0 new messages