Suppose I have an object that I want to add/multiply/subtract, etc. In this case, I want to have an element-wise multiply (basically, I just run through and multiply the 1D array by another, but I would like to interact with it later on as if it is a 2D array).
This tends to be a situation where I would like to write something like:
D * (A * B * C * ...) * ...
Below, I have an example that is compatible with it:
struct Matrix{
double * matrix_arr
long size;
long rows;
long cols;
...
Matrix elementwise_product(Matrix B){
...
return new_matrix_C;
}
}
Whereupon I overload the multiply operator, and everything is ok.
The only problem is that I understand the copy constructor is called with respect to objects returned by value, and I am creating objects which may be large.
I could do everything by allocating memory in higher scope, and performing the calculations with a pre-allocated result array, but then I lose the D*(A*B*C*...)
syntax which is very desireable.
How do I both get the syntax, and avoid the copy constructor?