Polymorphism needs to happen through a reference or pointer. At the extremes of your example, you don't know if you are pointing to 5 Child objects, or 5 Base objects (or I assume some further derived type).
One funny thing you could do is that if you need an array of size N, then you need to allocate 2N objects total, to account for both extremes (user choosing all objects of type A, user choosing all objects of type B). But this probably isn't practical, so something better is needed.
Alternatively, you can think about the data more, and figure out where you can perhaps just have the data itself serve as polymorphism instead of dealing with pointers and such. But this ultimately depends on the specifics of the problem, and the data you have.
For example, in your example, the difference between the child and the base is that the child has a factor of 5 applied to it. So why not just make that part of the data of your class?
1 2 3 4 5 6 7 8 9 10
|
class Foo {
public:
int base_value;
int factor;
int getNum()
{
return base_value * factor;
}
};
|
Then, adjust factor based on user input (could be 1 or 5). Again, I realize your actual use case might be more complicated than this, but it's a starting point that you could explore.
In other words, if you can't allocate more memory on the fly, then you need to make sure the memory you have at the start is the "greatest common denominator" of all the possibilities you wish to handle.
_________________________________________
Take what I say with a grain of salt; I am not an embedded programmer and I don't know the details of what people in that field have to deal with. I know other embedded programmers that still use polymorphism in their code, but they allocate everything they'll possibly need up front at the beginning of the program, and the resources used are relatively beefy for an embedded system.