Answers for "operator overloading outside class"

C++
0

operator overloading outside class

//Overloading an operator ouside a class
//Note that unlike when it's done inside a class
//the + operator requires 2 arguments instead of 1
//this is because there is no "this" object to be the 
//default lvalue

class Vector2
{
public:
    float x, y ;
} ;

Vector2 operator+( const Vector2& v1, const Vector2& v2 )
{
    Vector2 ans ;
    ans.x = v1.x + v2.x ;
    ans.y = v1.y + v2.y ;
    return ans ;
}
Posted by: Guest on July-23-2021

Code answers related to "operator overloading outside class"

Browse Popular Code Answers by Language