operator overloading outside class

Solutions on MaxInterview for operator overloading outside class by the best coders in the world

showing results for - "operator overloading outside class"
Evaline
28 Jul 2019
1//Overloading an operator ouside a class
2//Note that unlike when it's done inside a class
3//the + operator requires 2 arguments instead of 1
4//this is because there is no "this" object to be the 
5//default lvalue
6
7class Vector2
8{
9public:
10    float x, y ;
11} ;
12
13Vector2 operator+( const Vector2& v1, const Vector2& v2 )
14{
15    Vector2 ans ;
16    ans.x = v1.x + v2.x ;
17    ans.y = v1.y + v2.y ;
18    return ans ;
19}
20