i have plenty of idea's in mind:
since the inventory items are of type (Object*
), you can try dynamic_cast
another approach is, define an additional class (Ex: handHeld
), let Weapon
classes inherit Object
virtually, and handHeld
can inherit both Weapon
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 (
), 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: