Can someone please explain me how to make move semantics with 2d array of floats in a right way? I tried something like that:
struct Matrix {
    Matrix& operator=(Matrix&& rhs) noexcept {
        if (this == &rhs) {
            return *this;
        }
        for (size_t i = 0; i < rows; ++i) {
            delete[] matrix[i];
        }
        delete[] matrix;
        matrix = rhs.matrix;
        for (size_t i = 0; i < rows; ++i) {
            matrix[i] = rhs.matrix[i];
            rhs.matrix[i] = nullptr;
        }
        rhs.matrix = nullptr;
        return *this;
    }
    ~Matrix() {
        for (size_t i = 0; i < rows; ++i) {
            delete[] matrix[i];
        }
        delete[] matrix;
    }
    int rows;
    int cols;
    float** matrix;
};
but in a line rhs.matrix = nullptr matrix became nullptr and I don't know how to fix it.
I tried to just change pointers in inner arrays but outer pointer leave with no changes. But I think it is wrong way because programm logic became incorrect.
Aucun commentaire:
Enregistrer un commentaire