lhs 和 rhs 通过常量引用 (const Type&) 传递。
CVector operator+ (const CVector& lhs, const CVector& rhs) {
CVector temp;
temp.x = lhs.x + rhs.x;
temp.y = lhs.y + rhs.y;
return temp;
}
最佳答案
当要复制的数据是轻量级(廉价)时(例如,对于 int、float、char 等,按值传递就可以了)
该参数在函数/运算符实现中被观察到(即,它是一个输入只读参数)
作为这两点的总结,您将得到我们将明白为什么我们应该通过常量引用传递。
为了清楚地理解,请参阅 http://www.cs.fsu.edu/~myers/c++/notes/references.html
关于c++ - 运算符重载函数的参数什么时候应该通过常量引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27783504/