The canonical implementation of the matrix class in modern C++ :
template <typename Y> struct Matrix { public: Matrix(std::size_t width, std::size_t height) : holder_(new Y[width * height]), width_(width), height_(height) { } const Y& operator()(std::size_t i, std::size_t j) const { // Можно и 'throw', но подсадит производительность. assert(i < width_ && j < height_); return *(holder.get() + j * width + i); } Y& operator()(std::size_t i, std::size_t j) { assert(i < width_ && j < height_); return *(holder.get() + j * width + i); } // Остальные операции. private: std::unique_ptr<Y> holder_; std::size_t width_; std::size_t height_; }
I also note that to optimize complex calculations, you can use their proxying with the help of expression templates , which very well underlines the advantages of patterned C++ . Implementation details (and much more, of course) can be found at boost :: ublas.