📜  类外的运算符重载 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:48.535000             🧑  作者: Mango

代码示例1
//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 ;
}