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 :(

Recommended Answers

All 7 Replies

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

Make sure your 1st derived print is virtual, if your second derived is derived from your first derived.

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 :(

Care to elaborate for posterity's sake? What was the issue?

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 :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.