I just learnt the basics of operator overloading. After which I wrote the following code for vector addition of two points in a plane.
#include <bits/stdc++.h>
using namespace std;
struct point{
int x, y;
point operator+(point b){
point c;
c.x = x + b.x;
c.y = y + b.y;
return c;
}
};
int main()
{
point a, b, c;
a.x = 1, a.y = 2, b.x = 3, b.y = 4;
c = a + b;
cout<<c.x <<" "<< c.y;
return 0;
}
However most of the other operator overloading examples I find are coded very differently, example answers to this question. Even though I am getting the correct output using this, is there a cause for concern when I do it this way by declaring a new variable, or leaving out const
?