My question is about returning by reference in a function. For example , I have the code:
main.cpp
class Vector{
public:
Vector(int a , int b){
x = a;
y= b;
}
Vector() { }
int x = 1;
int y = 1;
};
Vector& operator+(const Vector& lvec ,const Vector& rvec ){
Vector resvec;
resvec.x = lvec.x + rvec.x;
resvec.y = lvec.y + rvec.y;
return resvec;
}
int main(){
Vector vecone(1,2);
Vector vectwo(1,2);
Vector resultvec = vecone + vectwo;
cout<<endl<<"X:"<<resultvec.x<<endl<<"Y:"<<resultvec.y;
}
It runs and works very well, however , I don't seem to understand the purpose of the reference operator ( & ) in the operator overloading function , yet i've seen it in many source code's containing operator overloading functions. The program seems to run very well when I dismiss the operator, so my question is - what's the purpose of returning by reference in a funcion? and does it serve a special objective in the code I presented?