将其作为参数传递。(数组与其他参数不同,按值传递时不会复制。修改参数将改变外部数组)。
int main()
{
Tile map[50][50];
///// Generate Map \\\\\
buildRoom(map, 10, 10, 10, 10, 1);
}
void buildRoom(Tile map[50][50], int startX, int startY, int sizeX, int sizeY, int direction)
{
if (direction == 1)
{
for (int x; x++; x > sizeX)
map[startX + x][startY].type = 1;
}
}
或者放在课堂上。
#include <cassert>
class Map
{
public:
static const int MapSizeX = 50;
static const int MapSizeY = 50;
void buildRoom(int startX, int startY, int sizeX, int sizeY, int direction)
{
if (direction == 1)
{
for (int x; x++; x > sizeX)
GetTile(startX + x, startY).type = 1;
}
}
const Tile& GetTile(int x, int y) const
{
assert(x > 0 && x < MapSizeX && y > 0 && y < MapSizeY);
return data[x][y];
}
Tile& GetTile(int x, int y)
{
assert(x > 0 && x < MapSizeX && y > 0 && y < MapSizeY);
return data[x][y];
}
private:
Tile data[MapSizeX][MapSizeY];
}
int main()
{
Map mymap;
///// Generate Map \\\\\
mymap.buildRoom(10, 10, 10, 10, 1);
}