Hello, I am having trouble with a Polymorphism issue using container classes.
I have a longwinded and shortwinded version of my question:
Shortwinded version:
How can I store base class objects in a container class like a vector or list while still being able to access the individual subclass (virtual) functions? In my program, it is as if the functions aren't virtual.
Longwinded version:
I have a class "Poisson" which has several subclasses (say, "Predator and Prey").
I would like to address specific functions of one of these sublasses without knowing which subclass I'm dealing with.
For a specific instance of the class, I would (for example) do this via:
___________________________________________
Poisson thing1*;
Poisson thing2*;
...
thing1 = new Predator();
thing2 = new Prey();
...
thing1.think(); // calls Predator::think();
thing2.think(); // calls Prey::think();
thing1.move();
thing2.move();
...
___________________________________________
In my program, the elements are stored in some data structure and each program loop cycles through the elements. Since the elements themselves are constantly coming in and out of existence, I'd like to use a linked list type structure to store them. So I thought I'd try using container classes instead of throwing together my own list.
My problem is that when I add these elements to the list, they are all stored as the base class and I can not address the subclass' individual (virtual) functions.
Can anyone help me to understand how to store a <list> or <vector> of objects which inherit a common base class object such that I can address the subclass functions individually? In other words, how can I store a list of objects with the same functionality as the above code?
I've attached some sample code which shows what I'm doing now and the problem that arises.
Thanks for your time!
- Andrew
/* ************************************************** **** **
* Example Code *
************************************************** ******* */
Poisson *herbert;
Poisson *carnie;
Poisson *fernie;
vector<Poisson> vec;
int main(int argc, char *argv[])
{
// Create Objects which are each some subclass of Poisson class.
herbert = new Predator('r',12,42,"Roberto");
carnie = new Prey('y',23,6,"Kate");
fernie = new Tree('y',23,6,"Fern");
// Push objects on to list.
vec.push_back(*herbert);
vec.push_back(*carnie);
vec.push_back(*fernie);
// Cycle through list and reference abstract objects.
unsigned int index;
for (index = 0; index < vec.size(); index++)
{
Poisson *temp = &vec[index];
cout << temp->getName() << endl;
// This returns Poisson::getName() instead of (say) Prey::getName();
}
}