Kel*_*bon 0 c++ optimization overloading
哪种方式更有效?this->X 或只是 X 有什么区别吗?我认为它的“无效”版本 bcs 编译器不需要调用构造函数或 smth,只需添加。
void operator+=(vect right)
{
this->x += right.x;
this->y += right.y;
}
void operator+=(vect right)
{
x += right.x;
y += right.y;
}
vect& operator+=(vect right)
{
x += right.x;
y += right.y;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
如果您关心效率,请先不要将复杂类型作为值的参数。
vect& operator+=(vect const& right)
{
x += right.x;
y += right.y;
return *this;
}
Run Code Online (Sandbox Code Playgroud)
this->x只是一个普通的x意思是一样的。它们根本不影响运行时。最后,返回,vect&因为它是惯用的。