|
-
March 20th, 2009, 08:13 PM
#1
Array of pointers
Hi
I am current ly using dynamically allocated pointers in a little simulation I am coding .
Code:
pos.Initialize(3,0,3);
TEnt* a=new TEnt(pos);
What it does is initialize a position and creates a new instance.
I now want to test my simulation with a large number of entities in which case it becomes hard for me to instance it one by one
How does a array of pointers work. Can someone give me a example ?
(I need to have a randomly choosen position for each of the entity )
-
March 20th, 2009, 08:25 PM
#2
Re: Array of pointers
First of all, it sounds like you want your 'array' to be of dynamic length. If this is the case, I highly recommend you use a vector instead of array. If using an array, you might do something like this:
Code:
// Creates the array
// Have MAX_LIST_LENGTH defined beforehand, or just use literal here:
TEnt list[MAX_LIST_LENGTH]; // ahoodin is right. put * symbol between 'TEnt' and 'list' on this line
// loop through the array, creating and adding elements
for(int i = 0; i < MAX_LIST_LENGTH; i++)
{
pos.Initialize(x,y,z);
list[i] = new TEnt(pos);
}
However, if you decide to use a vector, it might look something like this:
Code:
#include <vector>
...
std::vector<TEnt> list;
for(int i = 0; i < NUMBER_OF_ITEMS; i++)
{
pos.Initialize(x,y,z);
list.push_back(*(new TEnt(pos)));
}
Or, I could be totally off base here. Let me know if this helps.
Edit: ahoodin (below) is right. I forgot we were working with pointers. Simply add the * symbol in that case.
Last edited by Etherous; March 21st, 2009 at 01:12 PM.
-
March 20th, 2009, 08:36 PM
#3
Re: Array of pointers
Also, I put x, y, and z in my code example. But you would likely replace those with whatever function is generating your random positions.
-
March 21st, 2009, 12:36 PM
#4
Re: Array of pointers
Well if you don't change your syntax to use STL, which you may or may not care to do, i have come a few questions that should answer your questions:
Q1: How do I declare an array of pointers:
or
Code:
TEnt *a[] = {TEnt1,TEnt2,TEnt3,TEnt4};
Q2: How do you initialize an array of pointers
Code:
for (n = 0; n < nMax; n++)
a[n] = new TEnt(pos);
or:
Code:
TEnt *a[] = {TEnt1,TEnt2,TEnt3,TEnt4};
Q3: How do I dereference an element of a pointer array (one of the pointers):
Q4: How else should I manage an array of pointers:
Code:
for (n = 0; n < nMax; n++)
delete a[n]
but only after your done with them.
Last edited by ahoodin; March 21st, 2009 at 01:18 PM.
ahoodin
To keep the plot moving, that's why.

-
March 21st, 2009, 08:51 PM
#5
Re: Array of pointers
Thanks for the reply...
works great
-
March 21st, 2009, 09:00 PM
#6
Re: Array of pointers
Glad to help. Which post helped?
Update should be either:
Code:
TEnt *a[] = {&TEnt1,&TEnt2,&TEnt3,&TEnt4};
TEnt *a[] = {pTEnt1,pTEnt2,pTEnt3,pTEnt4};
where pTEnt1 is a pointer to TEnt1.
Last edited by ahoodin; March 21st, 2009 at 09:11 PM.
ahoodin
To keep the plot moving, that's why.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|