c++ - Accessing child function, while using parent class -


i doing assignment university course , me , partner have problem. program making game.

we have several classes, inherit base class, called creature. these enemies player needs deal , run own ais. there 4 different types of child classes, within namespace creature(including parent, creature), 1 class having special functions needs. class called bunny.

now, job call ai functions needed. problem is, not know what class calling out, such, when ask game board tell me creature get.

all enemies saved pointers so, in game board squares:

    struct square     {            // pointers morso class, enemy saved         creature::creature* creature;         //undeeded stuff removed      }; 

now, , fine until need access special functions. pupu multiply if conditions filled. such, in pupu there few functions need call make sure carries out it's act correctly.

however, here comes problem.

i call our board class give me creature in coordinates give it.

void gameengine::gameengine::runai() {     creature::creature* creature= null;      for(unsigned int y = 0; y < dimy; y++)     {         for(unsigned int x = 0; x < dimx; x++)         {             coordinate target;              target.setx(x);             target.sety(y);              creature= board_->returncreature(target);              //if there creature in target, run ai             if(creature!= null)             {                  //if is, check special procedures                 if(creature->returntype() == "bunny")                 {                     bunnyreproduce(creature);                 }                  creature->ai();             }          }//for x      }//for y } 

now, :

void gameengine::gameengine::bunnyreproduce(ccreature::creature* creature) {  //checks bunny if( creature->returntype() != "bunny"){ return; }  //check there bunny near creature->checkformate();   } 

the problem is, creature, @ point, can't call checkformate, public member of bunny, not creature. need make virtual function creature?

i tried making checkformate creature::bunny, since original value try give creature class, can't so. need to create empty virtual function in creature class , override it bunnyclass?

i running qt creator 2.7.0, qt 5.0.2.

you should add virtual function reproduce creature class , implement in bunny or other creature may later add game. creature reproduce in it's own way. don't need check creature type in case. since if have non reproducible creatures, may implement reproduce empty method nothing.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -