2

I ran into a problem when adding derived objects into an array of (abstract) base pointers. To simplify what I'm trying to do, here is an example:

Base** baseArray = new Base*[3];
baseArray[0] = new derived1(param,param);
baseArray[1] = new derived2(param,param);
baseArray[2] = new derived3(param,param);

Now, I have yet to get my virtual Print() working to print out the baseArray elements but using the vs2012 debugger I can only see baseArray[0] in baseArray. The other two are just gone.

But I guess my main question is that, should the example above work. If yes, would it be too much to ask of what could have happened. If not, is there a (better) way?

Edit: Thank you all!

cppStud
  • 29
  • 3

2 Answers2

3

Your code is correct. Given a dynamically allocated array, Visual Studio debugger will only display the first element. This answer might help.

Community
  • 1
  • 1
Marco Guerri
  • 932
  • 5
  • 11
2

Yes, it works just fine. The debugger is lying to you - probably because the static type of baseArray is pointer to pointer, not pointer to array of 3 elements.

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64