Looking for some help with arrays and pointers and explanation of what I am trying to do.
I want to create a new array on the heap of type Foo* so that I may later assign objects that have been created else where to this array. I am having troubles understanding what I am creating exactly when I do something like the following.
Foo *(*f) = new Foo*[10];
Also once I have created my array how do I access each element for example.
(f + 9)->fooMember(); ??????
Thanks in advance.
The parentheses in the declaration are unnecessary, so this is the same as:
In any case, the
new Foo*[10]allocates space for tenFoo*s and leaves them uninitialized. It returns a pointer to the initialFoo*in the array (the zeroth element), which you assign tof.To access elements of the array, you simply use subscripting:
Remember that anything you create using
new[]must be freed once when you are done with it by callingdelete[]on the pointer. For example:This does not delete the elements pointed to by the
Foo*s in the array. If you createFooobjects usingnew, you mustdeletethem before you delete the array. For example, to free the element we created above: