Operator overloading C++ outside class
- by bobobobo
Well, so there are 2 ways to overload operators for a C++ class
INSIDE CLASS
class Vector2
{
public:
float x, y ;
Vector2 operator+( const Vector2 & other )
{
Vector2 ans ;
ans.x = x + other.x ;
ans.y = y + other.y ;
return ans ;
}
} ;
OUTSIDE CLASS
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 ;
}
In C# apparently you can only use the OUTSIDE class method
The question is, in C++, which is "morer-correcter?" Which is preferable? When is one way better than another?