I have two classes A and B. The product A*B should be of type B.
Since variables of type B will occupy great amounts of memory, I need to avoid operations like B_1 = B_1 + (A_1 * B_2)
, where (A_1 * B_2)
would be temporarily allocated. In other words, I need B_1 += A_1 * B_2
.
I have tried this:
struct A {
int a;
};
struct B {
double b[size];
void operator += (pair<A*, B*> & lhsA_times_rhsB){
A & lhsA = *lhsA_times_rhsB.first;
B & rhsB = *lhsA_times_rhsB.second;
b[lhsA.a] += rhsB.b[lhsA.a];
}
};
inline pair<A*, B*> operator*( A& lhsA, B& rhsB){
return make_pair(&lhsA, &rhsB);
};
int main(){
A A_in;
B B_in, B_out;
pair<A*, B*> product = A_in*B_in;
B_out += product; //this works!
B_out += A_in*B_in; //doesn't work!? (Compiler: "No viable overloaded '+='")
return 0;
}
Why is
B_out += product
okay butB_out += A_in * B_in
is not?Is there a better way to implement the
+=
-operator for this use? Maybe without defining the helper objectpair<A*, B*>
?