我正在學習C++的類,我碰到這段代碼,我想知道什麼參數被用於。參數和params用法在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;
}
你不需要在你的重載參數,因爲它是一個成員函數。僅當operator +是非成員函數時才使用它。 – DumbCoder
@DumbCoder什麼? –
@Let_Me_Be什麼? – DumbCoder