Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hi all, hope you're all well.

 

A general question regarding constructors and destructors.

 

I can see (I think) why constructors are useful: in that they are automatically and immediantly called when an object is created, so you know that it'll always be done.

 

However I'm confused about what destructors are for. If unset removes objects and it all gets removed when the script finishes anyway what do destructors do? I've worked through Larry's example (script 6.7) so I can see the how and when but I'm missing the why!

 

Cheers

Paul

Link to post
Share on other sites

Destructors allow you determine any last actions that should be performed by an object. Maybe you'll save it in a database, notify another object that is gone, or something similar. The garbage collector is generally good at removing unused objects from memory, so it's often these extra, last minute things you want to do.

 

I have never programmed a destructor myself. They are not needed most of the time.

  • Upvote 1
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...