数据结构
每一行都存储在不同的连续内存中,每一行的第一个元素的地址存储在英国数组中。
class Matrix {public:int **data;size_t row;size_t col;Matrix(size_t row, size_t col) : row(row), col(col) {data = new int * [row];for (int i = 0; i < row; ++i) {data[i] = new int [col]{};}}};

数据结构二
所有数据存储在一段连续的内存中
class Matrix {public:int *data;size_t row;size_t col;Matrix(size_t row, size_t col) : row(row), col(col) {data = new int [row * col];}};
大五原则
如果一个类出现了析构函数,那么必须实现拷贝赋值函数,拷贝构造函数。C11 以后还需要实现移动构造函数,移动赋值函数。出现了析构函数说明类中有资源需要管理。
具体实现
#include <iostream>#include <cassert>class Matrix {int *data;size_t row;size_t col;public:Matrix(size_t row, size_t col) : row(row), col(col) {data = new int [row * col] {};}Matrix(const Matrix &other): row(other.row), col(other.col) {data = new int [row * col];for (int i = 0; i < row * col; ++i) {data[i] = other.data[i];}}Matrix &operator = (const Matrix &other) {if (&other == this) {return *this;}if (data != nullptr) {delete []data;}this->row = other.row;this->col = other.col;data = new int [row * col];for (int i = 0; i < row * col; ++i) {data[i] = other.data[i];}return *this;}Matrix(Matrix &&other) noexcept : row(other.row), col(other.col), data(other.data) {other.row = other.col = 0;other.data = nullptr;}Matrix &operator = (Matrix &&other) noexcept {if (&other == this) {return *this;}if (data != nullptr) {delete []data;}this->row = other.row;this->col = other.col;this->data = other.data;other.row = other.col = 0;other.data = nullptr;}Matrix &operator += (const Matrix &rhs) {assert(this->row == rhs.row && this->col == rhs.col);for (int i = 0; i < row * col; ++i) {data[i] += rhs.data[0];}return *this;}Matrix operator + (const Matrix &rhs) {assert(this->row == rhs.row && this->col == rhs.col);Matrix result = *this;result += rhs;return result;}class RowProxy {Matrix & mat;size_t x;public:RowProxy(Matrix & mat, size_t x) : mat(mat), x(x) {}int &operator[] (size_t y) {assert(y >= 0 && y < mat.col);return mat.data[x*mat.col + y];}};RowProxy operator[] (size_t x) {assert(x >= 0 && x < row);return RowProxy {*this, x};}friend Matrix operator * (const Matrix &lhs, const Matrix &rhs) {assert(lhs.col == rhs.row);Matrix result(lhs.row, rhs.col);return result;}Matrix &operator *= (const Matrix &rhs) {assert(this->col == rhs.row);*this = *this * rhs;return *this;}~Matrix() {delete []data;}};int main () {Matrix a {4, 5};Matrix b {4, 5};a[1][4] = 1;std::clog << a[1][4] ;return 0;}
- 加入一个 RowProxy 间接层来确保第二次下标访问可以判断出是否越界。
