操作符重载

wangkaixin-yy / 2023-08-25 / 原文

  • 再数据结构内重载它的操作符,一般会先写一个函数,然后再重载符号:返回类型+operator+重载符号(参数){//定义}
Vector2 Multity(const Vector2& other)const {
		return Vector2(x * other.x, y * other.y);
	}
	Vector2 operator*(const Vector2& other)const {
		return Multity(other);
	}
  • 对输出流的重载(两个参数,一个输出流,一个是输出的对象)
std::ostream& operator<<(std::ostream& stream,const Vector2& other){
    stream<<other.x<<","<<other.y;
}