Archived

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

C++ question, dynamic 2d array of pointers?

This topic is 5046 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

I want to allocate a grid of m x n size where each cell contains a pointer to a class. I know how to do this where you are creating a 2d array of classes but what do I do when I want them to be pointers to classes?

Share this post


Link to post
Share on other sites
class Test
{
};

Test A, B, C;

Test *Array[ 10 ];

Array[ 0 ] = &A;
Array[ 1 ] = &B;
Array[ 2 ] = &C;


If you want an array of different classes, then you need an abstract class.

Share this post


Link to post
Share on other sites
well I was going for something like this

Test *2DArray = new Test[n][m];

so then I can't just do...

2DArray[0][5] =&A;

Right?


[edited by - drastick on February 20, 2004 7:25:20 PM]

Share this post


Link to post
Share on other sites
That was alomst it but it helped me figure it out thanks.
I was able to get it to work with this

MyClass ***PtrGrid
int x = 10;
int y = 20;


PtrGrid = new MyClass**[y];
for(int j = 0; j < y; j++) {
PtrGrid [j]= new MyClass*[x];
}

int i, k;
for( i = 0; i < y; i++){
for(k = 0; k < x; k++){
PtrGrid [k][i] = NULL;
}
}


[edited by - drastick on February 21, 2004 2:27:30 AM]

Share this post


Link to post
Share on other sites