So I have been working on a Vector3D class, and to enhance usability, I have been overloading operators to define such functions as dot product cross product and such. The only trouble I've had is defining the scalar product.
In math, if a is a scalar (a single number) and v is a vector with components v1,v2,v3, a * v should multiply all the elements by a.
a * v
should do
a * [v1] = [a * v1]
a * [v2] = [a * v2]
a * [v3] = [a * v3]
In code, I want to write this:
1 2 3
double a = 4;
Vector3D myVector(1,1,1);
Vector3D result = a * myVector; //result should be (4,4,4)
The problem is that I don't know how to overload the * operator if the instance is on the right hand side of the * operator. I can define it the other way no problem.
1 2 3
Vector3D v(1,1,1);
v = v * 3; //Compile problem
v = 3 * v; //Compiles fine