How to pass a 2d array to a matrix class in c++ without using templates?

Viewed 45

i'm building a linear algebra library en c++ with learning purposes.

I want to make a constructor that takes the rows, columns and a 2D array for a matrix class without using templates or std::vector, but can't figure it out. I have this already done:

#include <iostream>
using namespace std;

class Matrix{
    private:
        int rows, columns;
        float **matrix;
    public:
        Matrix(int rows, int columns){
            this->rows = rows;
            this->columns = columns;
            this->matrix = new float*[rows]();
            for(int row = 0; row < rows; row++){
                this->matrix[row] = new float[columns]();
            }
        }
       float operator()(int row, int col){
            return this->matrix[row][col];
        }

        void print(void){
            cout << "\n";
            for(int row = 0; row < rows; row++){
                cout << "\n";
                for(int col = 0; col < columns; col++){
                    cout << matrix[row][col];
                }
            }
        }
    };

For initialize a matrix with, for example, Matrix A(3,3), printing it or using elements of it.

Now I would like to make one that takes a 2d array as an argument, something like Matrix A(2,2,a) where a is declared as float** a{{1,5},{7,8}}, but I can't figure out how to pass the array to the constructor. In C I would make a constructor like `Matrix (int rows, int cols, float matrix[rows][cols]), but in c++ that doesn't seems to work.

I don't want some crapy solution like input element by element, I want to make code readable, and declaration as simple as possible. Any advice?

0 Answers
Related