我在为乘法运算而重载乘法运算符时遇到问题。你怎么找到阵列的长度?我为我的for循环尝试了a.arr.length()但没有运气。
好奇,代码究竟知道何时应用重载 当两个对象相乘时?
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;
}