question on class pointers.

This is a discussion on question on class pointers. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to make an inventory system for a rpg game, and I'm having a hard time doing it. This ...

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

    question on class pointers.

    I'm trying to make an inventory system for a rpg game, and I'm having a hard time doing it. This is the idea of what I have so far:

    Code:
    class Item
    {
    public:
    void create_item(int amount){quantity += amount;}
    string name;
    int quantity;
    } potion = {"Potion", 0}, antidote = {"Antidote", 0};
    
    class Inventory
    {
    public:
    
    string* name;
    int* quantity;
    }inventory[100];
    
    void fill_inventory()
    {
     inventory[0].name = &potion.name;
     inventory[0].quantity = &potion.quantity;
     inventory[1].name = &antidote.name;
     inventory[1].quantity = &antidote.quantity;
    }
    
    void display_inventory()
    {
      for (int i = 0; i< 100; i++)
      if (*inventory[i].quantity > 0)
        cout << "Item# " << i+1 << " " << *inventory[i].name << " " << *inventory[i].quantity << endl;
    }
    So the idea is that each value in my inventory array points to an item name and quantity.
    Then display_inventory() will show the user all of the items they "have" ( more than 0). To add an "item", all I would have to do is call that objects add function, and the Inventory pointers would "update" to show the correct quantity;

    Is there a better way I can have the Inventory members point to their corresponding Item members? I wish I could just do inventory[0] = &potion; and the compiler would know to make inventory[0].name = &potion.name, and inventory[0].quantity = &potion.quantity

    And my second question is: I'm going about this all wrong aren't I? Haha.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    As you said yourself, make a class pointer. You seem not to need the Inventory class at all. Just an array of Item pointers
    Code:
    Item[100]* inventory;
    is enough, isn't it?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by C_ntua View Post
    As you said yourself, make a class pointer. You seem not to need the Inventory class at all. Just an array of Item pointers
    Code:
    Item[100]* inventory;
    is enough, isn't it?
    Item[100]* inventory; //this line gets errors for me.
    Item* inventory[100]; //this way works though, what's the difference?

    I need each value in inventory[] to point to the name and quantity of an Item object;
    I would like to be able to cout << *inventory[0].name << *inventory[0].quantity;
    I'm just not sure how to accomplish that.

    Edit:
    Oh, so I can access object members and member functions through pointers. I just need to use '->' instead of '.'
    Last edited by Xanderbeard; 05-28-2010 at 09:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class and pointer question
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2006, 10:00 AM
  2. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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