Jump to content
  • Advertisement
Sign in to follow this  
utilae

How do I create an array of pointers?

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am using C++. I have an std list of 'objects'. And I want to create a one dimensional array where each element in the array is a pointer to an 'object' in the std list of 'objects'. Below I am trying to loop through the list of 'objects' and setting each element of the array to point to an 'object' in the list.
//create array with number of cells on nonscrollable side of grid specified
CObject *ArrayOfObjectPointers;
ArrayOfObjectPointers=new CObject[ArrayWidth*ArrayHeight];

//loop through list of buttons and set button pointers in array
list<CObject>::iterator itObject=lstObjects.begin();
int y=0;int x=0;
while(itObject!=lstObjects.end())
{
					
	ArrayOfObjectPointers[(y*ArrayWidth)+x]=(*itObject);
	itButton++;x++;

	if(x==ArrayWidth)
	{
		y++;
		x=0;
	}
					
}


Is anyone able to help? I'm not sure that I am doing this right.

Share this post


Link to post
Share on other sites
Advertisement

CObject *ArrayOfObjectPointers;
ArrayOfObjectPointers=new CObject[ArrayWidth*ArrayHeight];


is not an array of pointers. Try this instead:


CObject **ArrayOfObjectPointers;
ArrayOfObjectPointers=new (CObject*) [ArrayWidth*ArrayHeight];


that will get you your array of pointers.

Share this post


Link to post
Share on other sites
Quote:
Original post by wendigo23

CObject **ArrayOfObjectPointers;
ArrayOfObjectPointers=new (CObject*) [ArrayWidth*ArrayHeight];


that will get you your array of pointers.


Thats not even correct either, get rid of the brackets.

@utilae your already using std::list so why are you not using std::vector.

Share this post


Link to post
Share on other sites
Quote:
Original post by snk_kid
@utilae your already using std::list so why are you not using std::vector.

It's personal preferance. I just want to use an array (I have been using vectors though).

Quote:
Original post by snk_kid
Thats not even correct either, get rid of the brackets.

So should it be like this:

CObject **ArrayOfObjectPointers;
ArrayOfObjectPointers=new CObject* [ArrayWidth*ArrayHeight];

Share this post


Link to post
Share on other sites
Quote:
Original post by utilae
Quote:
Original post by snk_kid
@utilae your already using std::list so why are you not using std::vector.

It's personal preferance. I just want to use an array (I have been using vectors though).


Fair enough, as long as your aware thats what counts [smile].

Quote:
Original post by utilae
So should it be like this:

CObject **ArrayOfObjectPointers;
ArrayOfObjectPointers=new CObject* [ArrayWidth*ArrayHeight];


Indeed [grin].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!