运算符实际上是函数,因为早先使用函数做运算不方便阅读,因此出现了运算符,本质是函数。
struct vector1{
float x,y;
vector1(float x,float y)
:x(x) y(y) {}
vector1 Add(const vector1& other) const
{
return vector1(x+other.x , y+other.y);
}
//重载
vector1 operator+(const vector1 &other) const
{
return Add(other);
}
};
重载<<运算符
std::ostream& operator<<(std::ostream& stream,const vector1 &other)
{
stream<<other.x<<" , "<<other.y;
return stream;
}