Logical question with classes

I have a base class called Object, and two classes drivied from Object called Weapon and Armor. I want a vector that acts like an inventory that can hold both Weapon and Armor objects, but I'm sure how to do this.

If I do
std::vector<Object*> inventory;
the vector wouldn't know about anything about Weapons or Armor class. That means anything I put in the drivied classes won't show up when I access the members of inventory. Only Objects. But if I did
std::vector<Weapon*> inventory;
the vector wouldn't know anything about Armor, and I'd rather not have two vectors for inventory.

Is there a better way to do this? I'll add source code if you'd like, but I don't think it's needed.

Thanks a bunch!
closed account (Dy7SLyTq)
you can do vector<Object*> inventory; the vector doesnt have to know about the class but the Object class can be used for both
But what if I wanted to use members from a weapon or piece of armor? Then I'd have to access the vector, but the vector only knows about members from the Object class; not Weapon or Armor class. So there's no way on calling the function ReturnDamage() (Only in the Weapon class) because it's not IN the Object class, and that's what the vector is made of.
If you read the tutorial on this website, it says:

One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential.

Hence, if you declare inventory as a vector of type Object* as suggested by DTSCode, you can make it point to any of the Object, Weapon or Armor type objects.
i have plenty of idea's in mind:

since the inventory items are of type (Object*), you can try dynamic_cast :
http://www.cplusplus.com/doc/tutorial/typecasting/

another approach is, define an additional class (Ex: handHeld), let Weapon and Armor classes inherit Object virtually, and handHeld can inherit both Weapon and Armor.
i actually suggest this approach.
i couldn't find a tutorial on virtual inheritance, if someone can mention a good link, it would be appreciated.

you can also use both previous approaches:
declare the inventory as (vector<Object*>), but when you allocate memory for it make sure to allocate it of type (handHeld).
when you want to access the members of the derived classes, use dynamic_cast.

another approach is, make the object class be the derived and the other two are the base, but i think this is against the "general to specific" principle, so i can't advise this approach.

the approach i advice:
use runtime polymorphism, declare all the needed methods for an object as virtual, and let the derived classes override them as needed.
i'm not experienced in runtime polymorphism (i'm still a beginner), so i don't know if you shall use dynamic_cast in this approach.

choose whatever suits your project.

EDIT: i forgot to insert a link on polymorphism:
http://www.cplusplus.com/doc/tutorial/polymorphism/
Last edited on
the approach i advice:
use runtime polymorphism, declare all the needed methods for an object as virtual, and let the derived classes override them as needed.


yep. definately the way.

the classic Shape example:
http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

1
2
3
4
5
6
7
8
9
 // store the address of Rectangle
   shape = &rec;
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   shape->area();


note that in both cases he does "shape->area()" i.e. the shape doesnt know what itself is at compile time :)
Last edited on
Topic archived. No new replies allowed.