Problem with destructors

This is a discussion on Problem with destructors within the C++ Programming forums, part of the General Programming Boards category; Ok in my tree program, I have to use a reference to occupancy in my tnod struct. So In my ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    Problem with destructors

    Ok in my tree program, I have to use a reference to occupancy in my tnod struct. So In my destructor

    Code:
    ~TNode (void) {
      occupancy--;
      delete data;
    }
    I tried decrementing it but doesnt work because cant decrement an reference... So how do I decrement the Tnode occupancy (reference to the tree struct occupancy)? Also, to delete a TNode in my destructor do I only use delete data, which deletes the data in the tnode or do I also have to say free(this)?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    There's nothing wrong with decrementing a reference if it is a reference to a type that supports decrementing. So if occupancy is a long&, then that code should work. Why do you think it's not working?

    You do not need to free(this), since the destructor is being called because something else is already freeing the current object.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I tried decrementing it but doesnt work because cant decrement an reference...
    You mean, you're trying to change what it points to? You're right, you can't do that with a reference. You can with a pointer, though.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    the tree struct has a long occupancy
    and the tnode struct has a long & occupancy which references the tree occupancy. So, how would I decrement the tnode occupancy in a tnode destructor?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You can decrement a reference.

    That code should work. Why do you think it's not working?

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You can decrement a reference.
    You can decrement a reference (in fact you can do everything to a reference that you can do to a regular variable). But you can't change what it points to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Yes, but that fact is just confusing the issue. The OP wants to decrement the value it refers to, which is possible. There is nothing wrong with the posted code. A lot more information would be helpful in determining the problem.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    nvm guys i made a stupid mistake... thnxs for the help tho...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21