Advertisement

Arrays of Pointers!

Started by August 22, 2000 12:08 AM
6 comments, last by wise_Guy 24 years, 4 months ago
Okay, here''s a tricky question (well, probably not...) I am trying to use an array of pointers, NOT a pointer to an array. Normally this would be defined as such:

class thing{blahblahblah};

thing * array[500];
 
How do I define this dynamically? I don''t know how many there will be. Also I am reluctant to use a linked list because the number won''t change at all and so therefore I don''t need much manipulation - only at loadtime. I''m sure there must be a way of doing this using new or something... wise_guy
Try this:

typedef thing* thingPointer;
thingPointer * stuff = new thingPointer[500];

Advertisement
I can''t guarantee this''ll work because i''m away from my compiler but maybe try:

Thing* array[] = new Thing*[size];

40

www.databyss.com
www.omlettesoft.com

"Don''t meddle in the affairs of wizards, for they are subtle and quick to anger."
-40
I use something like this in my game-to-be...
    thing **my_pointers;my_pointers = new thing *[num_objects];for(int i=0;i<num_objects;i++)my_pointers<i> = new thing;    


//Ksero
I know, I know. I'm weak!!!

    #include<vector>class thing {/*...*/};typedef std::vector<thing*> vthing;int main(){  vthing thing_array;  while(!done) // or you run out of resources I suppose...  {    thing_array.push_back(new thing);  }}        

YAP-YFIO,
deadlinegrunt

Edited by - deadlinegrunt on August 22, 2000 2:28:01 AM

~deadlinegrunt

Yeah, I''d use the vector of pointers as well, because I''m a slack bitch.

Maybe use the resize method once rather than push_back for each object pointer, but the main point is avoiding as much work as you can, right?

-------------
squirrels are a remarkable source of protein...
Advertisement
if the value won''t change then what are you talking about "dynamically"?

int Elements;

//do calculations on Elements
//get the final data for Elements

thing * pThing[Elements] = new Thing;

If you are using C++, why not use the STL template list ?
You can chuck anything in there. You can even have lists of lists. Imagine just being able to add in a node as you need it, at run time or real time.

This topic is closed to new replies.

Advertisement