Archived

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

Silence_sws

Dynamic 3d arrays of pointers to classes : Declaration

Recommended Posts

Silence_sws    122
Hi all, I''m having a few problems with declaring dynamic 3d arrays of pointers to a class in C++. What I have is a class and within this class is a pointer to a 3d array (m_pGrid), the type of this point is another class. What I need is a way of declaring a 3d array of these pointers from 3 dimensions. I would like to be able to index the array with something like, m_pGrid[x][y][z].classvariblesorfunctions However I seem inable to do this. Any ideas on how I can declare this sort of array. Thanks in advance You cannot see what does not want to be seen.

Share this post


Link to post
Share on other sites
Ravyne    14300
If I understand your problem correctly I don''t think it is possible to do such a thing. However I could be wrong. After a little testing I found that this is a possible work-around:

yourclass *temp = m_pGrid[x][y][z]; // may have to resort to ptr arithmetic here.

temp->classvariablesorfunctions;

Ravyne, NYN Interactive Entertainment
[My Site][My School][My Group]

Share this post


Link to post
Share on other sites
sheepsteak    122

yourclass ***m_pGrid = new (yourclass**)[x];
for(int i = 0; i < x; ++i)
{
m_pGrid[i] = new (yourclass*)[y];
for(int j = 0; j < z; ++j)
{
m_pGrid[i][j] = new yourclass[z];
}
}

That should work. If not try using yourclass() instead of just yourclass

Check out this topic for the reason why you must do this.




[edited by - sheepsteak on August 11, 2003 6:19:37 AM]

Share this post


Link to post
Share on other sites
biovenger    122
If you want to do it with the built in types, without use of vectors or linked lists, this is the correct syntax.


Classname ***pointer = new Classname**[NUM_FIRST_DIMENSION];

for(int i = 0; i < NUM_SECOND_DIMENSION; i++)
{
pointer[i] = new Classname*[NUM_SECOND_DIMENSION];
for(int j = 0; j < NUM_THIRD_DIMENSION; j++)
pointer[i][j] = new Classname[NUM_THIRD_DIMENSION];
}


Now, it would be much easier to create the same setup with a vector, using the following syntax


#include <vector>
using namespace std;

vector<vector<vector<Classname> > > pointer;

// Now all elements can be accessed by simple

// pointer[i][j][k].classmethod() syntax



Hope it helps.

Share this post


Link to post
Share on other sites
evillive2    779
I found it much easier to use a single dimension array instead of making all of those calls to new and having to make sure I delete it in the right order.

to create it:
int *m_pGrid;
m_pGrid = new int[width*height*layers];


and to access it you use a member functions:
void setvalue( int x, int y, int layer, int value )
{
m_pGrid[x+(y*width)+(layer*width*height)] = value;
}

int getvalue( int x, int y, int layer )
{
return m_pGrid[x+(y*width)+(layer*width*height)];
}


and to delete it:
void DestroyGrid()
{
delete [] m_pGrid;
m_pGrid = NULL;
}


Now thats just pseudo code but I hope it conveys the idea. I did a lot of reading on multi dimensional arrays a while back and I found that C++ doesn't truly support multidimensional arrays and it ends up doing this kind of math anyway. I prefer this way because it allocates a single chunk of memory at once with one call to new and can be deleted in one call to delete.

[edit]
damn, sorry, forgot it was using pointers. It isn't much different with pointers but if you are still having trouble, e-mail me and I will send you my 3darray class to look at.

[edited by - evillive2 on August 11, 2003 11:16:19 AM]

Share this post


Link to post
Share on other sites
Silence_sws    122
Thanks everybody who has posted, all the posts were a great help. I tested many ways of creating the arrays based upon the suggestions and have eventually settled on using the new operator in the code

// Create the 3d grid array
m_pGrid = new cBlock**[m_sizeX];
for(int i = 0; i < m_sizeX; ++i)
{
m_pGrid = new cBlock*[20];
for(int j = 0; j < 20; ++j)
{
m_pGrid[i][j] = new cBlock[m_sizeY];
}
}

Share this post


Link to post
Share on other sites