In C++, Im trying to make a game board out of a 2d array of Tile objects, A class I made. It looks something like this:
class Board {
private:
Tile m_board[9][9]
public:
Board();
//Constructors, functions, etc
};
class Tile {
private:
char m_type;
//other class members, etc
public:
Tile();
char getType();
void activate(); //This does nothing.
};
class BreakTile: public Tile {
public:
BreakTile();
void activate(); //This does something.
What I want to happen is that I can go too any tile on the board class, check if it’s a break tile, and if it is, active it. So I have a function in the Board class that looks something like this:
void Board::activate(int y, int x)
{
if (m_board[y][x].getType() == 'B')
{
m_board[y][x].activate();
}
}
And that can activate BreakTiles. However, all it’s doing is calling the activate function from the tile class – presumably because this is an array of tiles – despite the fact that In the constructor of Board I define some tiles as break tiles so I know the tiles I’m looking at here are break tiles, I just don’t know how to get the computer to recognize that.
I’ve been trying to see if I can somehow cast the tile into a break tile, but I have no idea how I would even begin to do that, I’m fairly new to C++.
Lucas Alexander is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3