inheritance and pointers

I've written 2 classes that inherit from another class, and written a class that has an array of pointers of the same type as the parent class. in the array of pointers, I have set them to point to child class objects that all have their own state variables and methods other than the parents. like this:


animal *array_of_animals[50]

for(int i = 0; i < 20; i++)
{
*(animal + i) = new hunter("lion", 20);
}
for(int i = 20; i < 50; i++)
{
*(animal + i) = new prey("monkey", 20);
}

the parent class has a pure virtual function, with implementation methods in the child classes. however, when I try to access one of the objects and call the pure virtual function it works for the first few elements but then I get a segmentation fault. I've found that when I decrease the length of the names when declaring the child objects that it takes longer for the fault to occur, e.g. setting "lion" to "l". does anyone know why this is happening, any help would be appreciated.
Last edited on
Instead of *(animal + i) you can simply write animal[i]. It's an array.

does anyone know why this is happening
Well, it probably depends on how you store the names, but you did not show that code/class.
Topic archived. No new replies allowed.