面试题答案
一键面试class Vector {
public:
double x;
double y;
Vector operator+(const Vector& other) const {
Vector result;
result.x = x + other.x;
result.y = y + other.y;
return result;
}
};
选择返回值类型为Vector
,是因为向量加法的结果仍然是一个向量,这种返回值类型直观地反映了操作的语义,与向量加法在数学上的定义一致,方便调用者后续对结果向量进行操作,例如继续参与其他向量运算或使用向量的属性。