0
class Matrix
{
private:
    int rows;
    int cols;
    int** Mat;


public:
    Matrix(){}

    Matrix(const int& rows, const int& cols):rows(rows),cols(cols)
    {
        Mat = new int* [cols];
        for (int i = 0; i < rows; i++)
        {
            Mat[i] = new int[cols]();
        }
    }
Matrix  operator +(const Matrix& other)const 
    {
        Matrix temp(rows, cols);
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    temp.Mat[i][j] += other.Mat[i][j] + Mat[i][j];
        return temp;
    }

My question is regarding how I'd use shared_ptr and make_shared to replace int** Mat? and, after that, how do I use it in the constructor and operator+? It's a custom matrix class that should add matrices

  • 2
    Why `std::shared_ptr` and not `std::unique_ptr` ? – Richard Critten May 14 '20 at 11:58
  • 1
    This question and answers may be relevant: https://stackoverflow.com/questions/53038457/what-is-the-best-modern-c-approach-to-construct-and-manipulate-a-2d-array – Galik May 14 '20 at 12:06
  • It's a question regarding shared_ptr specifically unfortunately.It's a follow-up on using table pointers to make a custom matrix class, so I can't change the code outside of using shared_ptr – Andrei Rustior May 14 '20 at 12:15
  • That is an unfortunate constrain, because `shared_ptr` is a poor choice in this situation. – Eljay May 14 '20 at 12:23

1 Answers1

0
#include <iostream>
#include <memory>
#include <cassert>

class Matrix {
private:
    int rows;
    int cols;
    std::shared_ptr<std::shared_ptr<int>> Mat;

public:
    Matrix() = default;

    Matrix(const int& rows, const int& cols) : rows(rows), cols(cols)
    {

        Mat.reset(new std::shared_ptr<int>[rows], [](std::shared_ptr<int>* p) { delete[] p; });
        for (int i = 0; i < rows; ++i)
        {
            Mat.get()[i].reset(new int[cols], [](int* p) { delete[] p; });
        }
        for (int i = 0; i < (*this).rows; ++i)
            for (int j = 0; j < (*this).cols; ++j)
                (*this)[i][j] = 0;
    }

    int* operator[](const int& index) const
    {
        return Mat.get()[index].get();
    }

    Matrix(const Matrix& other) : cols(other.cols), rows(other.rows)
    {
        Mat.reset(new std::shared_ptr<int>[rows], [](std::shared_ptr<int>* p) { delete[] p; });
        for (int i = 0; i < rows; ++i)
        {
            Mat.get()[i].reset(new int[other.cols], [](int* p) { delete[] p; });
        }

        for (int i = 0; i < other.rows; i++)
            for (int j = 0; j < other.cols; j++)
                (*this)[i][j] = other[i][j];
    }

    Matrix& operator=(const Matrix& other)
    {
        if (Mat != other.Mat && cols == other.cols && rows == other.rows)
        {
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    (*this)[i][j] = other[i][j];
            return *this;
        }
        else
            return *this;
    }

    Matrix operator+(const Matrix& other) const
    {
        Matrix temp(rows, cols);
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                temp[i][j] += other[i][j] + (*this)[i][j];
        return temp;
    }

    friend std::ostream& operator<<(std::ostream& os, Matrix& m)
    {
        for (int i = 0; i < m.rows; ++i)
        {
            for (int j = 0; j < m.cols; ++j)
            {
                os << m[i][j] << "  ";
            }
            os << std::endl;
        }
        return os;
    }
};

int main()
{
    Matrix a(2, 2);
    a[0][0] = 1;
    a[0][1] = 1;
    std::cout << a << std::endl;

    Matrix b(2, 2);
    b[1][1] = 1;
    b[1][0] = 1;
    std::cout << b << std::endl;

    b[1][0] = 9;
    Matrix c(a);
    c[0][0] = 6;
    std::cout << c << std::endl;

    Matrix d = b;
    std::cout << d << std::endl;

    Matrix e = a + b;
    std::cout << e << std::endl;

}

I have solved it myself in the end :D