1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| template<typename T> class Grid { public: Grid(size_t width=kDefaultWidth, size_t height=kDefaultHeight): mWidth(width), mHeight(height), mCells(width, std::vector<std::optional<T>>(height)) {}; virtual ~Grid() = default;
std::optional<T>& at(size_t x, size_t y) const;
static const size_t kDefaultWidth = 10; static const size_t kDefaultHeight = 10; private: size_t mWidth, mHeight; std::vector<std::vector<std::optional<T>>> mCells; };
template <typename T> std::optional<T>& Grid<T>::at(size_t x, size_t y) const { if (x >= Grid<T>::mWidth || y >= Grid<T>::mHeight) throw std::out_of_range(""); return mCells[x][y]; }
|