In C++ if I want an array of pointers so that I may have them point to different objects at a latter stage what does the syntax look like.
EDIT
I need to clarify what I am trying to do I guess. I have a class foo that has and add method. In the add method I take a reference to class bar. I want to save that reference into a pointer array of bar. The array of pointer bar will need to be expanded all the time this I have no problems with. It is creating and array on the heap of pointers so that I may assign bar objects to them later. What I have tried seems to fail as class bar does not have a default constructor which the compiler is complaining about. This lead me to think that I was creating and array of actual objects which I did not want to do.
Please no stl and no I do not want to hear about how you think this is crazy etc that is your opinion.
An array of pointers is useful for the same reason that all arrays are useful: it lets you numerically index a large set of variables. Below is an array of pointers in C that points each pointer in one array to an integer in another array. The value of each integer is printed by dereferencing the pointers.
datatype *pointername [size]; For example, int *p[5]; It represents an array of pointers that can hold 5 integer element addresses.
An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like: int *ptr[5]; // array of 5 integer pointer.
The pointer amount is initialized to point to total : double time, speed, *amount = &total; The compiler converts an unsubscripted array name to a pointer to the first element in the array. You can assign the address of the first element of an array to a pointer by specifying the name of the array.
What you want is:
Foo *array[10]; // array of 10 Foo pointers
Not to be confused with:
Foo (*array)[10]; // pointer to array of 10 Foos
In either case, nothing will be automatically initialized because these represent pointers to Foos that have yet to be assigned to something (e.g. with new).
I finally "got" pointer/array declaration syntax in C when I realized that it describes how you access the base type. Foo *array[5][10];
means that *array[0..4][0..9]
(subscript on an array of 5 items, then subscript on an array of 10 items, then dereference as a pointer) will access a Foo object (note that []
has higher precedence than *
).
This seems backwards. You would think that int array[5][10];
(a.k.a. int (array[5])[10];
) is an array of 10 int array[5]
. Suppose this were the case. Then you would access the last element of the array by saying array[9][4]
. Doesn't that look backwards too? Because a C array declaration is a pattern indicating how to get to the base type (rather than a composition of array expressions like one might expect), array declarations and code using arrays don't have to be flipflopped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With