#include
#include
//运算符及其重载
//重载:允许在程序中定义或更改运算符的行为,相当于函数
//利用 定义操作符号函数的功能 对代码进行操作,简化代码
struct Vector2//默认public
{
float x,y;
Vector2(float x,float y)
:x(x),y(y){}
Vector2 Add(const Vector2& other)const
{
return *this+other;//逆向引用
}
//利用操作符
Vector2 operator+(const Vector2& other)const
{
return Vector2(x+other.x,y+other.y);
}
Vector2 Multiply(const Vector2& other)const
{
return Vector2(x*other.x,y*other.y);
}
Vector2 operator*(const Vector2& other)const
{
return Multiply(other);
}
//比较运算符的重载
bool operator==(const Vector2& other)const
{
return x==other.x&&y==other.y;
}
};
//重载的符号的最初定义
std::ostream& operator<<(std::ostream& stream,const Vector2& other)
{
stream<
//重载的符号的最初定义 (重点)
std::ostream& operator<<(std::ostream& stream,const Vector2& other)
{
stream<
return stream;
}