c++ - Getting derived class from base method -
i have 3 classes entity
, character
, item
. when
character * player = new character(qrectf(0, 0, 50, 50), "player", thescene); player->setvelocityx(30)->setattackpoint(10);
the compiler tells error: 'class entity' has no member named 'setattackpoint'
. how can make entity* setvelocityx(qreal vx);
return character
pointer or item
pointer?
.
class entity : public qobject, public qgraphicspolygonitem { q_object public: entity(); entity(qreal x, qreal y, qreal w, qreal h, qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); entity(qrectf position, qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); entity(qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); entity* setvelocityx(qreal vx); // etc } class character : public entity { q_object public: character(qreal x, qreal y, qreal w, qreal h, qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); character(qrectf position, qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); character(qstring tag, qgraphicsscene *scene = 0, qgraphicsitem *parent = 0); character* setattackpoint(int attackpoint); //etc } class item : public entity { q_object public: enum itemtype{ consummable, special, weapon }; item(qstring name, qreal x, qreal y, qreal w, qreal h, qstring tag = "item", qgraphicsscene *scene = 0, character *parent = 0); item* setowner(character* newowner); //etc }
if absolutely on type (otherwise use dynamic_cast , check resulting pointer non-nullptr):
reinterpret_cast< character * >( player->setvelocityx(30))->setattackpoint(10)
also, based on code, couldn't go instead or order of function calls significant?
player->setattackpoint(10)->setvelocityx(30)
Comments
Post a Comment