Again, I have a little problem with nasty multidimensional arrays. I have a class, let's call it Base with a method virtual int get(). From this class, I derive a new class TheClass which overrides the method get().
Now I have the following (reduced) code:
1 2 3 4 5 6 7 8 9 10 11 12 13
Base **MDArray = new TheClass*[somenumber]; //(*)
for(short i=0; i<somenumber; i++)
MDArray[i] = new TheClass[somenumber];
for(int i = ...)
for(int j= ...)
{
MDArray[i][j] = TheClass(ConstructorParams)
}
DoSomeThingWithValue=MDArray[i][j].get();
...
If I do it like this, I get a compiler error that I am trying to do an invalid conversion at the line marked by (*). I tried an alternative version, which reads:
1 2 3 4 5 6 7 8 9 10 11 12
Base **MDArray = new Base*[somenumber]; //(*)
for(short i=0; i<somenumber; i++)
MDArray[i] = new Base[somenumber];
for(int i = ...)
for(int j= ...)
{
MDArray[i][j] = TheClass(ConstructorParams)
}
DoSomeThingWithValue=MDArray[i][j].get();
...
Actually, this version compiles but it does not work: instead of calling the intended get() method of TheClass, it only calls the get method of the base class.
I would be pleased and delighted if anyone might come up with ideas.
You were closer in option 2, but you are forgetting a very important thing: Polymorphism requries pointers or references, and your bidimensional array is of type Base, not of type Base* or Base&. You need:
1 2 3 4 5 6 7 8 9 10
Base ***MDArray = new Base**[someNumber];
for (int i = 0; i < someNumber; i++)
MDArray[i] = new Base*[someNumber];
for (int i = 0; i < someNumber; i++)
for (int j = 0; j < someNumber; j++)
{
MDArray[i][j] = new TheClass(....);
DoSomething = MDArray[i][j]->get();
}
Base **MDArray = new Base*[somenumber]; //(*)
for(short i=0; i<somenumber; i++)
MDArray[i] = new TheClass[somenumber];
The first is wrong, because TheClass* is not a child of Base* (even though TheClass is a child of Base).
The second creates actual Base objects. It seems that runtime type information is only used when the real type is not known (which is not the case). Also, assignment doesn't overwrite type information.
@hamsterman: Your code suffers from object slicing. The multidimensional array has to be of pointers or references.
Also the OP's second one.
EDIT: Actually, your code doesn't compile, right? You are assigning the result of the new operator (which is a pointer of type TheClass) to a slot in the array which is of type Base (not pointer to Base, just Base).
ANOTHER EDIT: My bad. No, you are not slicing objects. You are doing it OK if the array were of a single dimension. That's what confused me. OP: Hamsterman's array is OK, but it is a one-dimension array. Mine is 2 dimensions.
LOL! Every time I hit submit I see it differently!! Darn! Let me really check it out.....
OK, I think I got it now. Hamsterman's code should compile, as each individual slot of MDArray can hold a pointer to Base. The problem comes when the code tries to access an individual member. It will incur in a form of object slicing, but furthermore, it most likely plain crashes when accessing item index 1 or above because most likely the byte size of Base is different than that of TheClass. So there!