Sign in to follow this  
utilae

How do I create an array of pointers?

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this