简体   繁体   中英

How do I store and use different classes in a vector in c++

I just want to start by saying I'm still decently new to coding, and I've been trying to do research but just got stuck, as I can't find exactly what I am looking for.

I am trying to make my first game, a simple text adventure game. I was trying to make a player's inventory so I started using a vector of custom classes. After some research I found that I needed to make a base class of multiple classes.

Just for reference, I am using

vector<item*> inventory;

class Map : public item
{
public:
    void print();
    virtual string getName(){return name;};
private:
    string name = "Map";
}; 


class Compass : public item
{
public:
    virtual string getName(){return name;};
private:
    string name = "Compass";
};


class item
{
public:
    virtual string getName(){return name;};
private:
    string name = "item";
};

this is in my header file, and I have cpp files that expand on it.

When storing it I found out that I have to add virtual functions to get the name, so I added a name data member and made a getName() function and made it virtual. Eventually I was able to pull the items out and get the name, but then I found a new problem I couldn't find the answer to.

EDIT:

after using inventory.push_back(&map) , trying to call (inventory[0])->print() gives me the error of "no member of print in item"

end edit

My map class has a print() function, and i need to use it from the vector. Would I have to create a virtual print() function in my item class just so it can transfer down using the virtual part, or is there an easier way of doing this?

sorry if this is simple or a stupid fix, when I was doing research all I found was people trying to find names and things, but i couldn't find how to access a method without having it in the base class.

In order to use any subclass of a base class polymorphically, you would pass around a pointer or reference to the base class, and call its virtual member functions through that.

You should carefully consider alternatives, such as storing Plain-Old-Data in your vector instead of a class for everything. How many methods will actually have a different implementation between subclasses?

One data structure you could use would be a std::vector<std::unique_ptr<item> > . You would create items such as std::unique_ptr<item>(static_cast<item*>(new compass)) , or as a shortcut, inventory.emplace_back(...) . This handles the memory management automatically. You can then call methods such as inventory[0]->getname(); . But, be sure the destructor ~item() is virtual , even if it's just virtual ~item() = default; . Otherwise, the wrong destructor would get called!

Another alternative would be std::variant , the STL replacement for tagged unions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM