你能解释一下返回值、值的引用和值的常量引用之间的区别吗?
值:
Vector2D operator += (const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
非常量引用:
Vector2D& operator += (const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
常量引用:
const Vector2D& operator += (const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
这有什么好处呢?我理解传递const引用到函数的意义是你想确保在函数内部不修改引用所指向的值。但是我对返回const引用的含义感到困惑。为什么返回引用比返回值更好,为什么返回const引用比返回非const引用更好?