-2

I'm making a text-based RPG kind of thing in C++, and I'm making the Inventory class. Here is my code:

class Inventory {
    public:
        int size;
        Item items[];

        Inventory(int size): size(size) {

        }
};

In the constructor, I need to set items to an array of Items with the length of size. I know in Java, I'd just do this.items = new Item[size], but I've been looking for a while now, and even on the cplusplus.com tutorial, it doesn't say how I'd do this. Is there a way? If so, how?

Zac Garby
  • 3
  • 2

2 Answers2

2

C++ doesn't support this regarding raw arrays. The intended thing in c++ is using a std::vector<Item> instead:

class Inventory {
    public:
        int size;
        std::vector<Item> items;

        Inventory(int size): size(size), items(size) {

        }
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

Using std::vector<Item> is the one and only sane solution here.

In a comment to another answer, you are adding this piece of information:

One problem, however, is that I need to limit the size. Can I do this with vectors or would I have to manually limit it (e.g not letting it add more items to the vector once it's too big)

A std::vector by itself is pretty limitless. Its only real limit is the available memory.

But you should get into the habit of encapsulating container classes anyway. Usually, a container class offers way more functionality than you will ever need for one particular use case. For example, your items member variable may never require rbegin(), cend(), shrink_to_fit(), difference_type, get_allocator() or pop_back(), just to name a few examples.

It is therefore good practice to create a custom data type which offers only those operations which you really need, and implement the custom data type in terms of std::vector. It then becomes trivial to implement additional constraints.

Example:

#include <vector>
#include <string>
#include <stdexcept>
#include <exception>
#include <iostream>

// just a super-simple example:
struct Item {
    std::string name;
};

// wraps std::vector<Item>:
class Items {
public:
    Items(int max_size) :
        max_size(max_size),
        items()
    {
    }

    void Add(Item const& item) {
        if (static_cast<int>(items.size()) == max_size) {
            throw std::runtime_error("too many items");
        }
        items.push_back(item);
    }

    Item Get(int index) const {
        return items[index];
    }

private:
    int max_size;
    std::vector<Item> items;
};

int main()
{
    Items items(5);
    try {
        items.Add({ "sword" });
        items.Add({ "shield" });
        items.Add({ "torch" });
        items.Add({ "axe" });
        items.Add({ "boots" });
        std::cout << items.Get(3).name << "\n";
        items.Add({ "gloves" });
    } catch (std::exception const& exc) {
        std::cerr << exc.what() << "\n";
    }
}

Notice that this example uses exceptions to handle the error. This may not be suitable to your use case; you may consider assert instead.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62