I'm not sure how to go about this one. I have a class "Grid", which has a 2D vector of "GridTile" objects. GridTile is a base class from which "UiTile" inherits. I want to write the Grid class so that the 2D vector can store GridTile tiles or any type that inherits from GridTile.
Would templates be the best way to do this?
This is what I have attempted so far. I've read that the implementation must be in the same file as the declaration, so it is all in Grid.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Base class 2D grid
template <class GridTile> class Grid
{
public:
// boring fields etc not included...
vector< vector<GridTile> > tileTable; // 2D array of cells
};
template <class GridTile>
void Grid<GridTile>::drawCellsAll_Wireframe()
{
// body of code not included...
}
I've tried this with both GridTiles and classes that inherit from GridTile and it seems to work ok. Am I about right in my approach?