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!

2
  • What do you mean - is baseArray[1] an invalid pointer? Commented Apr 11, 2015 at 7:01
  • Reasonable question. Have +1'd and closed as dupe only to tidy-up. Commented Apr 11, 2015 at 23:42

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I think you are right. I should asked sooner. :)
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.