你能向我解釋返回值,值的引用和常量引用值之間的區別嗎?C++返回值,引用,常量引用
值:
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 Vector2D& operator += (const Vector2D& vector)
{
this->x += vector.x;
this->y += vector.y;
return *this;
}
我這是好處嗎?我明白const引用傳遞給函數的意義,因爲您要確保不要修改引用指向函數內部的此值。但是我被返回const引用的含義弄糊塗了。爲什麼返回引用比返回值更好,爲什麼返回const引用比返回非const引用更好?
您的C++書的可能重複 –