visual c++ - C++ - trying to point each sub-class to the main class in order to contain all information in a vector -
basically, im writing small database/inventory system game. item class , sub-classes (weapon, armor, consumable, etc) , working. vector inventory written within separate inventory class (allows more 1 inventory - i.e. enemies can have them too!) , @ point, inventory far written - there still no drop function, etc.
but im taking 1 step @ time because learning experience.
basically believe in case, pointers answer. code seems work (have not tried yet)
weapon debugdagger{ 1, "debug dagger", "debug dagger given help", 25 }; armor datacloak{ 2, "data cloak", "data cloak given help", 10 }; item *pitem = &debugdagger; weapon *pweapon = &debugdagger; inventorymanager playerinv; playerinv.pickupitem(pweapon);`
in case, pickupitem(item*)
taking static item type - base class function, weapon being added inventory sub-class (class weapon : public item{}
)
the way written seems hack solution, easier if write pointer class functions each item created in sub-class automatically create pointer of base class can add inventory subsystem. there way within class? each member gets pointer created automatically upon class creation? when try use name variable in constructor this, not work, seems have create member before making pointer, dont see how @ same time anticipating new member being created.
i mean, i'm other more effective ideas std::vector inventory; inventory - think function can accept members of class , record them far more effective in long run, not believe possible - think creating pointers item sub-classes still necessary - seeing as, really, items, each sub function having single specifier being used own function, weapon has attack, consumable has restoration, etc -and integers, same each other almost, function reasons, equipping, using, classifying, each separate type suppose couldve made specialitem subclass cover all, ambiguous, , leave little room innovation should specific type need modification later.
i dont see if there alternative. if there direct me , read voraciously until find understanding of it.
not sure if got point, pointer subclass (weapon) is already pointer base class (item).
so can store items (weapon : item, arnmor : item, ...) in your
std::vector<item*> inventory
Comments
Post a Comment