0

I have been trying to copy an object from a method to another object which has a dynamic allocated memory data member, i.e a matrix. But the object can't access created memory inside my Matrix class.

#ifndef DS_MATRIX_H
#define DS_MATRIX_H

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::cin;
using std::endl;

class Matrix 
{
    public:
        Matrix(string remark = "", unsigned int rows = 1, unsigned int cols = 1);
        ~Matrix();
        void set_dimensions(unsigned int, unsigned int);
        void initialize();
        Matrix multiply(const Matrix&) const;
        void output() const;
    private:
        unsigned rows;
        unsigned cols;
        string remark;
        double **matrix;
        void create_matrix(unsigned int, unsigned int);
};

Matrix::Matrix(string remark, unsigned int rows, unsigned int cols)
{
    this->remark = remark;
    create_matrix(rows, cols);
}

Matrix::~Matrix() 
{
    for (int i = 0; i < rows; i++) {
        delete[] matrix[i];
    }
    delete[] matrix;
    matrix = nullptr;
}

void Matrix::set_dimensions(unsigned int rows, unsigned int cols)
{
    if (rows < 0 && cols < 0) 
        throw "Number of rows or columns should be > 1";

    this->rows = rows;
    this->cols = cols;
}


void Matrix::create_matrix(unsigned int rows, unsigned int cols)
{
    set_dimensions(rows, cols);

    matrix = new double * [rows];
    for (int i = 0; i < rows; i++) {
        matrix[i] = new double [cols];
    }
}

void Matrix::initialize()
{
    cout << "enter the (" << rows << " x "
        << cols  << ") " << remark << " : \n";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cin >> matrix[i][j];
        }
    }
}

void Matrix::output() const
{
    cout << "Matrix " << remark << " : \n";
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (j > 0 && j < cols) { cout << ", "; }
            cout << matrix[i][j];
        }
        cout << endl;
    }
}

Matrix Matrix::multiply(const Matrix& x) const
{
    Matrix result("product", rows, cols);

    if (cols != x.rows) 
        throw "Multiplication is not possible.";

    for (int i = 0; i < rows; i++)
        for (int j = 0; j < x.cols; j++) {
            result.matrix[i][j] = 0.0;
            for (int k = 0; k < cols; k++) 
                result.matrix[i][j] = result.matrix[i][j] + matrix[i][k] * x.matrix[k][j];
        }
    return result;
}

#endif
    
// Driver code
int main()
{
    Matrix matA("matrix A", 3, 3), matB("matrix B", 3, 3);
    Matrix prod;
    matA.initialize();
    matB.initialize();
    prod = matA.multiply(matB);
 
    prod.output();    // Fails to access the dynamic memory created inside my class
    return 0;
}

How can I safely copy an object in this manner and still be able to access the data member which was created dynamically (it should also be copied as well). Thanks!

chrispin2510
  • 1
  • 1
  • 2
  • You need to write a copy constructor and copy assignment operator for your Matrix class. This is known as 'following the rule of three'. – john Feb 19 '21 at 11:18
  • Also you might what to look at [this answer](https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) for a simple way to write the copy assignment operator – john Feb 19 '21 at 11:20
  • Thank you I will be checking that, it seems its exactly what I am currently missing – chrispin2510 Feb 19 '21 at 11:24
  • To my suprise, I went to youtube and I was watching some "professor Hank Stalica" explaining the same concept you told. Once he showed on how an object can be initialize,, in one of the ways he uses ```Foo g; Foo h(g);``` I have tried it even without going any further and it worked. Could you please explain this a little bit? What I have just done is instatiating ```Matrix prod(matA.multiply(matB));``` and it worked. – chrispin2510 Feb 19 '21 at 12:34
  • well, I have gotten a clue. I needed a copy assignment operator, that's why ```Foo g = f;``` didn't work but ```Foo g(f);``` worked. Thank once again for the enlightment. – chrispin2510 Feb 19 '21 at 12:47

0 Answers0