- #1
yungman
- 5,755
- 293
I want to start a thread on Operator overloading for related question.
I watched this youtube video and I like the way Jamie King write the operator overloading function:
This is the program I copied from him:
He uses Vector operator+(Vector &left, Vector&right){}. then in main() result = operator+(v1,v2). To me, this is a lot more descriptive.
Is there anything wrong with writing like this instead of the conventional Vector operatort+(Vector&right){}?
I really find youtube video very helpful, much more so than reading online articles.
I watched this youtube video and I like the way Jamie King write the operator overloading function:
This is the program I copied from him:
C++:
#include <iostream>
using namespace std;
struct Vector
{int x; int y;};
//Below is another way to write operator function.
Vector operator+(const Vector& left, const Vector& right)
{ Vector ret;
ret.x = left.x + right.x;
ret.y = left.y + right.y;
return ret;
}
/*Vector operator+( const Vector& right)//normal way of writing operator function.
{ Vector ret;
ret.x = x + right.x;
ret.y = y + right.y;
return ret;
}*/
int main()
{ Vector v1; v1.x = 2; v1.y = 3;
Vector v2; v2.x = 1; v2.y = 4;
Vector result; //result = v1 + v2;
result = operator+(v1, v2);
cout << " result.x = " << result.x << " result.y = " << result.y << "\n\n";
}
He uses Vector operator+(Vector &left, Vector&right){}. then in main() result = operator+(v1,v2). To me, this is a lot more descriptive.
Is there anything wrong with writing like this instead of the conventional Vector operatort+(Vector&right){}?
I really find youtube video very helpful, much more so than reading online articles.