[C++] Array of Mixed Elements?
Hello everyone. I’m working on a small project which tries to simulate the interaction of someone buying/sellin different items at a store and then placing/taking them in/out of their inventory. Something commonly found in RPG’s and MMO’s.
I’m working with C/C++ and Objects. Each item is a different object (EX: HealthPotion, Dagger, etc…). I’m also treating the user’s inventory as an object (which will be created only once within the program). The inventory will have only 5 slots (this is just a small project). Each slot will be filled with one item (some items such as potions will be stackable). The problem I’m currently having is how to store all of the different items within the inventory.
I originally thought that I could store all of the items with in an array or some other container like a vector. Problem is the elements all need to be of the same type. I can’t place an object of type HealthPotion and an object of type Dagger into the same array.
So I either need to figure out a new way of storing all the different objects within the inventory or figure out how to make an array that allows you to place different object types in the elements. Such as a void array or something. I’ve searched google, msdn, and GameDev but I’ve yet to find a solution to my problem here.
So I’ve come to ask for the assistance of the GameDev community. Does anyone have any ideas on how to go about finding a solution to my problem? Thanks for your time.
This is where an array of base class pointers would come in handy.
All objects should be derived from this base class.
All objects should be derived from this base class.
There are two usual solutions:
a) Give all inventory items a common base class and store a collection of base class pointers to be used polymorphically.
b) Make all inventory items the same type, that is to say use a data driven approach instead of an inheritance based one:
a) Give all inventory items a common base class and store a collection of base class pointers to be used polymorphically.
b) Make all inventory items the same type, that is to say use a data driven approach instead of an inheritance based one:
Inventory Item: name: Health Potion size: 1 etcInventory Item: name: Dagger size: 1 etc
Something like...
Hope that helps :D
class BaseItem{ public: virtual void DoWhatItemDoes() = 0;};class PotionItem : public BaseItem{ public: void DoWhatItemDoes() { MakePlayerFaster(); }};BaseItem* ItemArray[10];PotionItem A_Potion;ItemArray[0] = &A_Potion;ItemArray[0]->DoWhatItemDoes();
Hope that helps :D
You can also store the base class pointer in a list or vector etc...
class CBaseObj{public: virtual void draw() = 0; // pure virtual};class CDagger : public CBaseObj{public: virtual void draw() { // draws dagger... }};class CPotion : public CBaseObj{public: virtual void draw() { // draws potion... }};class CBurger : public CBaseObj{public: virtual void draw() { // draws burger... }};int main(){ CDagger dagger; CPotion potion; CBurger burger; vector<CBaseObj*> objVector(3); // vector of base class pointers objVector[0] = † objVector[1] = &potion; objVector[2] = &burger; for(int i = 0; i < objVector.size(); i++) { objVector.draw(); } return 0;}
To fitfool:
To make your code more clear, if a base class function is virtual,
you should declare the derived class function as virtual for clarity.
This is not neccesary because it is done automatically but it makes it easier to read and understand when the classes are spread across multiple files.
To make your code more clear, if a base class function is virtual,
you should declare the derived class function as virtual for clarity.
This is not neccesary because it is done automatically but it makes it easier to read and understand when the classes are spread across multiple files.
Well I'll be damned. I go away for two hours and I come back to see all this, WHOOHOO! Sorry. Seems like I have a few different choices on how to proceed. I thought about using inheritance and a base class array, but I didn't because I wasn't aware that you could place derived objects within the array of the base type. I guess I better go review inheritance. ^_^
I want to thank everyone for their help. I really appreciate it. Thanks again for your time. Take care.
I want to thank everyone for their help. I really appreciate it. Thanks again for your time. Take care.
Quote:Original post by Koolchamp
I wasn't aware that you could place derived objects within the array of the base type.
You can't. If you try it, you will notice that the derived object gets "sliced" to a base object. You can however store pointers to derived objects in an array of a pointer-to-base type.
Oh, and your base class needs to have a virtual destructor.
[Edited by - DevFred on August 17, 2008 6:53:47 AM]
Quote:Oh, and your base class needs to have a virtual destructor.
Using virtual destructors is a goog practice.
They are only needed when the class heiarchy plays with dynamic memory.
I use them anyway... ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement