Question about destructors

This is a discussion on Question about destructors within the C++ Programming forums, part of the General Programming Boards category; I'm working on an inventory system for a game. Right now I have: Code: Struct Item() { string name; int ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    18

    Question about destructors

    I'm working on an inventory system for a game. Right now I have:

    Code:
    Struct Item()
    {
    string name;
    int value, quantity, type;
    int creator(int);
    };
    
    
    int Item::creator(a)
    {
      if (a == 1)
      {
      Item potion;
      potion.name = "Potion";
      potion.value = 5;
      potion.type = 1;
      potion.quantity++;
      }
    }
    So when the user buys, crafts, or finds a potion in the game, I will tell Item::Creator to make an object named potion, and assign its values. If the user where to use or sell all of their potions, should I have some code that will delete the potion object and its members to free up memory? Is that would I could use a destructor for? If so, how hard would it be to add a destructor in the above example?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,801
    You are more talking about dynamic memory allocation / deallocation then destruction. Basically you would use a vector or list to store the items and when the particular item was used or sold the item would be removed from the container, essentially freeing up the memory used by that item. Now if that item had any dynamic properties and those needed to be deallocated that is where your destructor would come into play.
    Woop?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by prog-bman View Post
    You are more talking about dynamic memory allocation / deallocation then destruction. Basically you would use a vector or list to store the items and when the particular item was used or sold the item would be removed from the container, essentially freeing up the memory used by that item. Now if that item had any dynamic properties and those needed to be deallocated that is where your destructor would come into play.
    In my above example. Is there a way to delete the object Potion if the user no longer has any? That would free up the memory used to store information about an object that I don't even need. Or am I confused? heh.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by Xanderbeard
    Is there a way to delete the object Potion if the user no longer has any? That would free up the memory used to store information about an object that I don't even need. Or am I confused?
    I suspect that you are confused. There is no object named Potion, but the object named potion is a local variable that will be destroyed as soon as control leaves the scope of the if statement in Item::creator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    You use a destructor if there is some action you must perform every time an object of that class is destroyed. There is nothing this class needs to do upon destruction, and you do not need a destructor for it.

    Other bugs present in this small snippet of code:
    The keyword 'struct' must not have a capital S.
    You haven't given 'a' a type.
    'creator' is defined as returning an int, but does not do so.
    You're incrementing an value that has not yet been initialised.
    'creator' does not touch member variables, yet it's not static. This factory method also has no way of returning and instance of class Item that it creates. (Not a bug but clearly a mistake nonetheless)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Xanderbeard View Post
    In my above example. Is there a way to delete the object Potion if the user no longer has any? That would free up the memory used to store information about an object that I don't even need. Or am I confused? heh.
    You are way too late with that. The item called potion is long gone -- it vanished as soon as you exited the item::creator function. Presumably you meant to work on the Item object you called it with, instead of creating a brand new item and throwing it away. So something like
    Code:
    int Item::creator(a)
    {
      if (a == 1)
      {
      name = "Potion";
      value = 5;
      type = 1;
      quantity++;
      }
    }
    will change the values of the item object. But this is not a constructor; the object must already exist.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    18
    Thanks mates, I see where I was confused now. That code I posted was just something I typed for the post-- I wasn't trying to run that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  2. Question about Constructors and Destructors with Pointers
    By specter in forum C++ Programming
    Replies: 14
    Last Post: 04-01-2010, 09:39 AM
  3. SDL buffer channels question
    By TriKri in forum Game Programming
    Replies: 3
    Last Post: 12-09-2009, 04:52 PM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

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