Pointer Matrix

M

Mark

hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filename);
}
};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually.... like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?
 
M

Mark

hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filename);
}

};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually.... like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?

the code below makes more sense, but still doesn't compile

Tile (*tile)[16][16];

public:
Map()
{
for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = &square;
break;
case 1:
tile[x][y] = &triangle;
break;
}
}
}
}
 
J

John Harrison

Mark said:
hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

http://www.sfu.ca/~mnb2/matrix.gif

and here's the relevant code I'm working with (doesn't work)

class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filename);
}
};

Tile square("img/square.png");
Tile triangle("img/triangle.png");

class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];

I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually.... like so

for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}

I can't seem to figure out the proper syntax...

also..how would I properly delete this?
delete [][]tile?

Why are you allocating this matrix? It's 16x16 always so there is no
need to allocate.

class Map
{
Tile* tile[16][16];
};

If you really did want to allocat then it would be

class Map
{
Tile* (*tile)[16];
Map()
{
tile = new Tile*[16][16];
}
};

This only works because the first dimension of your array is constant.

You delete the same way you delete any array allocation

delete[] tile;

john
 
M

Mark

Mark said:
hi, I need some help declaring a matrix of pointers. I made an image
to illustrate what I had in mind...

and here's the relevant code I'm working with (doesn't work)
class Tile
{
public:
SDL_Surface *img;
bool (*col)();
Tile(char* filename) {
img = IMG_Load(filename);
}
};
Tile square("img/square.png");
Tile triangle("img/triangle.png");
class Map
{
Tile ***tile;
Map()
{
tile = new *Tile[16][16];
I can't use tile = new Tile[16][16]; because then it actually tries to
construct each of the tiles, when I just want a matrix of pointers,
and I will assign each tile later individually.... like so
for(int y=0; y<16; y++)
{
for(int x=0; x<16; x++)
{
switch( rand()%2 )
{
case 0:
tile[x][y] = square;
break;
case 1:
tile[x][y] = triangle;
break;
}
}
}
}
I can't seem to figure out the proper syntax...
also..how would I properly delete this?
delete [][]tile?

Why are you allocating this matrix? It's 16x16 always so there is no
need to allocate.

class Map
{
Tile* tile[16][16];

};

If you really did want to allocat then it would be

class Map
{
Tile* (*tile)[16];
Map()
{
tile = new Tile*[16][16];
}

};

This only works because the first dimension of your array is constant.

You delete the same way you delete any array allocation

delete[] tile;

john

erm..i discovered a solution, hence the deleted posts. i wasn't sure
if it was necessary to allocate memory for them or not, and ideally i
don't want a constant 16x16 grid, that was just for sample. decided
to use a <vector<vector<Tile*> >
but thank you for the help :)
 
D

Default User

Mark wrote:

erm..i discovered a solution, hence the deleted posts.

For future reference, that doesn't really do much except remove them
from the Google archives. Most of the participants here are not reading
via GG, and your deletion doesn't necessarily affect the copies on
other servers. I'm not entirely sure if that sends a cancel message or
not.

In general, it's best not to try to delete them, as you get an
indeterminant state where some people see the referenced message and
others don't.





Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top