but can I do the other way around (using an **Base to hold an array of Derived)?
For instance:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class Foo { /* Stuff here */ };
class Bar:public Foo { /* Stuff here */ };
class Baz:public Foo { /* Stuff here */ };
int main()
{
Foo **f;
f=new Foo* [2];
f[0]=new Bar;
f[1]=new Baz;
return 0;
}
This code compiles, but can I be sure there will be no run-time errors, caused by something like the pointer arithmetic error described at the above link?
That's a tricky question. I think your example is exempt from the issue because your array is an array of pointers. Pointers will always be the same size. Their array was not of pointers but of varying-sized objects.
Regardless, in C++, you'd need to provide proof that you need to manage the array yourself rather than using an STL container. A vector< Foo * > is easier to manage and safer too.