I've been studying/reading about C++ for a long time now, but I've hardly written any. So despite knowing a good amount about modern C++ practice, I still don't have good design patterns down.
Here's my issue: Let's say I have a class Board
, which represents a game board (i.e., chess, go, etc). The underlying storage for this board is a std::array
.
There's another class GameRunner
which owns a Board
and is in charge of running the rules of the game, keeping score, etc.
The game board has a size defined by the user. In my head GameRunner was simply declared as such: GameRunner::GameRunner(unsigned size)
, and then size
parameter is likewise passed into the Board that is declared: Board::Board(unsigned size)
.
The problem is that std::array
must have a size statically determined. So I can't have a member variable of std::array
because I don't know at compile time what the size would be. I thought I could make a std::unique_ptr
as my member, and then have that point to a dynamically created array, but of course you need to define std::unique_ptr<std::array<???????>>
where the question marks denote how I obviously don't know the size of this array.
So my solution was to make Board have a template to pass in the size... so GameRunner
would have a Board<size>
, but then of course GameRunner needs to have a template as well, so now it's templates all the way down...
So what do I do here? Templates all the way down? Use a variable-size container even though the size should never change? I feel like there's something incredibly trivial I'm missing...