C++ How can I write a method that takes two matrices and calculates the number of rows and columns

I am writing a matrix handling class to get some practice with the language.

My first method is this:

template<typename T>
T* matrixClass<T>::CreateMat(unsigned int nRow, unsigned int nCol, T& num)
{   
    
    matrixClass::m_nRows = nRow; //number of rows
    matrixClass::m_nCol = nCol;  //number of columns

    matrix = new T[nRow * nCol];//memory allocation
    for (unsigned int row = 0; row < nRow; row++) //row selection N x m
    {
        for (unsigned int col = 0; col < nCol; col++)
        {
            matrix[col + row * nCol] = num;   //assigns data to columns n x M
            num += 1.1;
        }
    }

    return matrix;
}

I am trying to write a method in the same class that takes two matrices created by this method and finds its rows and columns .

Upvotes: 0

Views: 88

Answers (1)

Botje
Botje

Reputation: 30927

The typical shape of these kinds is something like

template <class T>
class Matrix {
  private:
  unsigned int m_nRows, m_nCols;
  T* m_Data;

  public:
  Matrix() = delete;
  Matrix(unsigned int nRow, unsigned int nCol, T* src = nullptr);
  Matrix(const Matrix& orig);
  Matrix(Matrix&& orig);
  Matrix& operator=(const Matrix& orig);
  Matrix& operator=(Matrix&& orig);
  ~Matrix();

  unsigned int rows() const { return m_nRows; }
  unsigned int columns() const { return m_nCols; }
}

... or make the constructors private and have createMat take over the role of constructing a Matrix instance. There are lots of edge cases to discover (or learn) with this approach, so have fun :)

Upvotes: 4

Related Questions