I know that it is a good idea to make as many non-member interfaces as possible, and I just realized that for my three-dimensional vector class "Vector3" I can move + =, - =, etc. statements outside the class, leaving only the constructors and copy assignment operator.
Question: what should this operator look like? I saw the canonical forms of many other operators and followed their advice, but I did not see the canonical forms of these operators. I have given what, in my opinion, should be lower.
Second question: what do these operators call? Arithmetic assignment operations?
Code (relevant):
class Vector3 { public: Vector3& operator+=(const Vector3& rhs); float x, y, z; }; Vector3& Vector3::operator+=(const Vector3 &rhs) { x += rhs.x; y += rhs.y; z += rhs.z; return *this; }
What I have changed so far:
class Vector3 { public: float x, y, z; }; Vector3& operator+=(Vector3& lhs, const Vector3& rhs) { lhs.x += rhs.x; lhs.y += rhs.y; lhs.z += rhs.z; return lhs; }
source share