for some vector arithmetics in 3 dimensions I defined a vector class with consturctors and I try to overload some operators:
class Vector3{
public:
typedef double d_type;
d_type x,y,z;
Vector3(d_type d_x, d_type d_y, d_type d_z){x = d_x; y = d_y; z = d_z;}
Vector3(){x=0;y=0;z=0;}
Vector3 operator +( Vector3 &v){return Vector3(this->x + v.x, this->y + v.y, this->z + v.z);}
Vector3 operator -( Vector3 &v){return Vector3(this->x - v.x, this->y - v.y, this->z - v.z);}
Vector3 operator +=( Vector3 &v){return Vector3(this->x += v.x, this->y += v.y, this->z += v.z);}
Vector3 operator -=( Vector3 &v){return Vector3(this->x -= v.x, this->y -= v.y, this->z -= v.z);}
};
if I understand it right, "&v" is a call by reference, only a reference is put on the stack, not the whole object Vector3 v. Is *v also possible as a call by reference? I have seen some other versions like:
inline Vector3 operator+(const Vector3& u, const Vector3& v)
{return Vector3(u.x + v.x, u.y + v.y,u.z + v.z);}
inline Vector3 operator-(const Vector3& v)
{return Vector3(-v.x, -v.y, -v.z);}
const Vector3& operator+=(const Vector3& v) {x+=v.x;y+=v.y;z+=v.z;return *this;}
const Vector3& operator-=(const Vector3& v) {x-=v.x;y-=v.y;z-=v.z;return *this;}
Why use "inline" and "const"? does it give a runtime boost? Is there a difference between "Vector3& v" and "Vector3 &v"? What are the advantages and disadvanteages of overloading the operators in that kind of way?