By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,403 Members | 953 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,403 IT Pros & Developers. It's quick & easy.

Pointer Matrix

P: n/a
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?

Mar 10 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mar 9, 6:19 pm, "Mark" <mnbaya...@gmail.comwrote:
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;
}
}
}
}

Mar 10 '07 #2

P: n/a
Mark wrote:
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
Mar 10 '07 #3

P: n/a
On Mar 9, 11:57 pm, John Harrison <john_androni...@hotmail.comwrote:
Mark wrote:
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
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 :)

Mar 13 '07 #4

P: n/a
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
Mar 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.