scalar * vector
Hi,
I'm stuck at this.
I can scale a vector by a scalar (float), but I can't do it in the opposite way.
1 2 3 4
|
vec2f a = vec2f(1.0f, 1.0f);
float b = 1.0f;
a = a * b; //this works
a = b * a; //this doesn't
|
Here's the function I'm using
1 2 3 4
|
vec2f vec2f::operator*(float num)
{
return vec2f(v[0] * num, v[1] * num);
}
|
how can I make it work both ways?
Best regards,
Yours3!f
ok whatever I solved it:)
I just needed to declare a non-member version of it:
vec2f operator*(float num, vec2f vec);
and define it as:
1 2 3 4
|
vec2f operator*(float num, vec2f vec)
{
return vec * num;
}
|
Make the operator a friend function, and overload it for (vec2f,num) AND (num,vec2f).
Topic archived. No new replies allowed.