#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3

    Explicitly calling the destructor?


    Hi.
    Is it OK to explicitly call the destructor?

    I have a Linked list class.
    I'm trying to implement the overload the assignment operator.
    so the first thing I did is checking for self assignment, the next thing I need to do is to empty the linked list from nodes (before copying data to the object that made the call).
    Can I call the destructor explicitly to do it for me?
    Can it raise any problems?

    and while I'm at it, can I call the Copy Constructer somehow to make the copy instead of duplicating code?

    Thanks in advanced.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    I'm not sure why you need to call the destructor without delete'ing the object. Same with the copy constructor.

    Its been quite a while, but I remember having had a situation where I would want to re-initialize an existing object in the same way as when I new'd it -- either that or I wanted two constructors for the object to perform the same initialization actions. Well, you know that a constructor may call another method. So I created an Initialize method and placed all the initialization code in it, then I called Initialize from the constructor(s). Same could be done for the destructor as well, I would think. So when you needed those actions performed without actually creating or destroying objects, just call those methods that the constructor and destructor call.

    Does that cover what you are needing? Or am I missing something?
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    If you call the destructor directly it will execute the destructor without releasing the memory allocated for the object -only that explicitly deleted by the destructor. If you later delete the object it will run the destructor again, the result of which will depend on what the destructor does. Worse, the object will still exist, and if you access it after calling the destructor that may be undesirable too.

    You would normally only call the destructor directly when using placement new.

    If you need an "emptyList" function, create one, and if the destructor must also empty the list call "emptyList()" from the destructor.

    You know of course that the C++ standard library already includes a linked-list container class?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Thanks for the help guys.
    apparently, calling the destructor with "delete object;" turned out to be a bad idea, since it destroyed the object altogether, after releasing the allocated memory, and that was not the result I was expecting.
    I couldn't access the object after it.
    so, as you suggested, I wrote a function that empty the linked list and restores it to its initial state.
    I also gave up on the idea of calling the constructor, as it turnes out, it wasn't so bad to recreate the object.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by so.very.tired
    calling the destructor with "delete object;" turned out to be a bad idea, since it destroyed the object altogether, after releasing the allocated memory, and that was not the result I was expecting.
    Really!? What did you expect? The clue is in the name!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    132
    Rep Power
    3
    Originally Posted by clifford
    Really!? What did you expect? The clue is in the name!
    I was expecting only a release of allocated memory.
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,117
    Rep Power
    1803
    Originally Posted by so.very.tired
    I was expecting only a release of allocated memory.
    If that happened where would the object exist!?

IMN logo majestic logo threadwatch logo seochat tools logo