I am having trouble overloading multiplication operator for the purpose of array multiplication. How would you find the length of the array? I tried a.arr.length() for my for-loop but no luck.
Just curious, how exactly does the code know when to apply the overloading when multiplying two objects?
class Box {
public:
int arr[5] = {1,2,3};
int sum;
int getarr() {
return sum;
}
// Overload * operator to add two Box objects.
};
int operator*(const Box& a, const Box& b) {
Box box;
Box d;
int sum = 0;
for(int x = 0; x<3;x++){
sum += d.arr[x] * b.arr[x];
}
return sum;
}
int main(){
int sum = 0;
Box box2; // Declare Box2 of type Box
Box box3;
sum = box2 * box3;
std::cout<<sum;
}