c++ - Calling virtual methods of different derived classes in a single array of pointers -


ok attempting make component/entity system game engine, , have base class component has virtual method update(), , different types of components derived single base class. store pointers these components in array, loop on array calling update method. when have 1 type of derived class works fine calls derived virtual method, once add different type of derived class starts using base update method. here's , example:

//hold pointers components component** components; 

//in system constructor initialize array of components void system::system() {     components = new component*[max_components](); } 

//inputlistener derived component    inputlistener* inputlistener; inputlistener= new inputlistener;  //playercontroller derived component playercontroller* playercontroller; playercontroller = new playercontroller;  //adds pointer components array system->add(inputlistener); system->add(playercontroller); 

//loops on array update method virtual method  system->update(); //calls base classes update method not derived 

from have read online seam should possible since array holding pointer not object should getting sliced base class. if indeed wrong assumption solution?

//hold pointers components std::vector<component*> components;  //inputlistener derived component    inputlistener* inputlistener; inputlistener= new inputlistener;  //playercontroller derived component playercontroller* playercontroller; playercontroller = new playercontroller;  components.push_back(inputlistener); components.push_back(playercontroller);  for( size_t = 0; < components.size(); ++i ){   component* cmp = components.at(i);   if( cmp ) cmp->update(); } 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -