Archived

This topic is now archived and is closed to further replies.

tordyvel

Array of pointers

Recommended Posts

Maybe I havn''t studied my c++ book enough, but I can''t seem to figure out how to make a dynamic array of pointers. i don''t want to do like: Object *pointers = new Object[some int]; because I just want the pointers. Or can you just do like: Object *pointers; pointers[0] = Object_one; pointers[1] = Object_two; pointers[2] = Object_three; Thanks for your help!

Share this post


Link to post
Share on other sites
If you want an array of pointers, there are two ways:

Object* arrayOfPointers[number_of_pointers_here];

But that will not allow any dynamics in the number of pointers, which I''d think you''d want:

Object** arrayOfPointers = new Object*[number_of_pointers_here];
arrayOfPointers[0] = new Object(constructor_parameters_here);
arrayOfPointers[1] = new Object(constructor_parameters_here);
arrayOfPointers[0]->blah = 1;

The second listing there will allow you to dynamically allocate how many objects you''d want. You could also work with just Object* I guess, up to you, but then it''s not really an "array of pointers" which is what you''re asking for ^_^

Share this post


Link to post
Share on other sites
If you want an example of an array of pointers.. look at the second argument that main takes. That is exactly an array of pointers to arrays of char.... However, I tend to go with fruny''s suggestion... use STL .

Share this post


Link to post
Share on other sites
Toss up between STL and a straight Object** -- depends on if you want the joys of templates in your project.

To set up a *dynamic* array of pointers, use a std::vector<object> pointers. I don''t use STL, so I don''t know J.S. on vectors.

To set up a *dynamicly generated* array of points, use "Object ** pointers = new Object *[kArraySize];", but don''t forget to nuke the memory later. Or if you need more, rebuild the array.

To "just" set up an array of pointers, use "Object * pointers[kArraySize];" Mind Porthios''s point about the immutable array length.

Personally, I''d use both an Object ** and an Object[], and use the pointer array to organize the Objects as I wanted to. You can still hit the main array''s indices by numbers, and yer prog will expect the memory to be needed -- skip some of those out-of-memory problems. This runs across the problem of not being able to adapt to attempts to spam yer engine with a mess of objects (think *craft unit limits), but that''s a design issue.

-"Sta7ic" Matt

Share this post


Link to post
Share on other sites
if you need a container thats fast for sequetial access (but not so good for random access)... that allows for cheap insertion/deletion.. then the std::list is a good choice. If you want to add to your list of object pointers... you can just:


std::list<CObject*> objects;
object.push_back(new CObject());


When you destory the object that contains your object list.. remember to iterate through, and deallocate the memory. There are plenty of nice methods on list objects, as well as automatic support for the algorithms. Have a search around for one of the many STL guides that are around. I''m sure you''ll be an STL convert .

Share this post


Link to post
Share on other sites