I've defined a matrix class consisting of a float** and an assignment operator. I would like to now the proper way to allocate a member of this class onto the heap, where the member is returned from a function. In function A I calling function B, which returns a matrix to function A. However, this matrix gets deleted before function A returns. Any help would be greatly appreciated.
mat broadcast(list a, list b); // function B
mat function A(){
mat *j = new mat(1, 1); // trying to allocate on heap
*j = broadcast(j1, j0);
//both *j and the rvalue broadcast are inexplicably deleted here
return j;
}
Assignment operator:
mat &operator=(const mat &other) {
clear(); //deletes any previous memory
this->arr = new float *[other.row_size];
for (int i = 0; i < other.row_size; i++) {
arr[i] = new float[other.col_size];
}
for (int i = 0; i < other.row_size; i++) {
for (int j = 0; j < other.col_size; j++) {
arr[i][j] = other.arr[i][j];
}
}
return *this;
}