这是从一篇C++教程中摘录的:
在运算符重载函数中,它创建了一个名为
链接到C++教程之一:
// vectors: overloading operators example
#include <iostream>
using namespace std;
class CVector {
public:
int x,y;
CVector () {};
CVector (int,int);
CVector operator + (CVector);
};
CVector::CVector (int a, int b) {
x = a;
y = b;
}
CVector CVector::operator+ (CVector param) {
CVector temp;
temp.x = x + param.x;
temp.y = y + param.y;
return (temp);
}
int main () {
CVector a (3,1);
CVector b (1,2);
CVector c;
c = a + b;
cout << c.x << "," << c.y;
return 0;
}
在运算符重载函数中,它创建了一个名为
temp
的局部变量,然后返回它。我很困惑,这是正确的方式吗?
operator+
通常是通过operator+=
实现的事实。 - chrisreturn (temp)
中的括号是必需的吗? - qed