I'm writting Matrix class and got to the point where I need to create an array of Array objects, but Array can't have constructor with no arguments ( it need m argument to allocate memory for it ). I searched and haven't found the solution, people only sugest doing it with Vector object, but I am not allowed to do it with anything other than arrays. Then a friend sent me the constructor for matrix that works for them, but even they don't know why it works.
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}
I don't get how this this->data[i]
is defined? [] operator isn't overloaded anywhere and data is only a single Array object not an array of Array-s. So why and how is this working and not an compile error?
Source files:
Header:
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void print() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
};
#endif //ROK04_ROK04_H ```
#ifndef ROK04_ROK04_H
#define ROK04_ROK04_H
class Array{
protected:
int* data;
int m;
public:
Array(int m);
Array& operator = (const Array& a);
virtual ~Array();
Array& operator+=(const Array& a);
virtual void setElem(int pos, int value);
virtual void stampa() const;
friend class Matrix;
};
class Matrix{
protected:
Array* data;
int n;
int m;
public:
Matrix(int n, int m);
void stampaj();
};
#endif //ROK04_ROK04_H
Cpp:
#include <iostream>
#include <ctime>
#include "rok04.h"
using namespace std;
Array::Array(int m):m(m),data( new int[m]() ){}
Array& Array::operator=(const Array& a) {
cout << "Usao" << this << endl;
for(int i = 0; i < m; i++){
data[i] = a.data[i];
}
return *this;
}
Array::~Array() {
delete[] data;
}
void Array::setElem(int pos, int value) {
try {
if(pos >= m){
cout << "Usao\n";
throw out_of_range("Index out of bounds!");
}
data[pos] = value;
} catch (out_of_range& oor){
cerr << oor.what() << endl;
}
}
void Array::print() const {
// cout << m;
for (int i = 0; i < m; ++i) {
cout << data[i] << " ";
}
cout << endl;
}
Array &Array::operator+=(const Array& a) {
int* temp = new int(m+a.m);
for(int i = 0; i < m; i++){
temp[i] = data[i];
}
for (int i = m; i < m+a.m; ++i) {
temp[i] = a.data[i-m];
}
delete[] data;
data = temp;
m = m+a.m;
return *this;
}
Matrix::Matrix(int n, int m):n(n),m(m) {
srand(time(0));
this->data=new Array(n);
for(int i=0; i<n; i++){
this->data[i].m=this->m;
this->data[i].data=new int[m];
for(int j=0; j<this->m; j++){
this->data[i].data[j]= rand()%10;
}
}
}