戻り値、値への参照、および値への const 参照の違いを説明してもらえますか?
価値:
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 参照を返すよりも優れているのはなぜですか?