Hi, i wish to store a mix of derived classes in one array.
so i make the array from the base class i.e.
Base* array[11];
and fill it with my classes.

The base class has a method.
Base::print();
this is overridden in all the child classes and used as follows,
array[0]->print();
array[1]->print();

my problem arises when array[x] is a 2nd derived class i.e.

base--->1stderived class--->2nd derived class

it will only access the print() of the 1st derived class.
i.e. the 2ndDerivedClass::print() is NEVER accessed by the program.

NOTE:
Base* one = new 2ndDerivedClass(..arguments..);
one->print();
WORKS FINE!!
the problem ONLY happens when the 2ndDerivedClass is stored in the array.

Please help :(

Can you please post your code? Your problem would be understood better that way...

Yes it is virtual.

I really cannot think why this is happening,
could the array be 'cutting' part of the 2ndDerivedClass off?

if the array only allocates memory for the Base class how come the 1stDerivedClass works?

Im very new to c++

Please post some code. We have no way of knowing what is happening if we can't look at the relevant portions of your code.

Cheers for trying to help, but solved now.

Rather embarrassingly it was a silly mistake by myself :(

In order to choose what particular classes to make I had a series of if else statements.

(you can prob see where this is going)

I had an if() rather than if else() so the class i wanted was 'overwritten'
by another threw the if(). Very foolish of me :(

This article has been dead for over six months. Start a new discussion instead.