Operator overloading - lhs, rhs?

G

Guest

Hi all,

I have a function:

mat4 operator * (const float scalar);

(matrix times integer)

Is there a way that I could multiply an int by a matrix, as opposed to only a matrix by an int?


Thanks!
 
R

Rolf Magnus

Hi all,

I have a function:

mat4 operator * (const float scalar);

I guess this is a member?
(matrix times integer)

Is there a way that I could multiply an int by a matrix, as opposed to
only a matrix by an int?

Yes, make it a non-member.

mat4 operator*(float scalar, const mat4& mat)
{
return mat * scalar;
}
 
H

Howard

Hi all,

I have a function:

mat4 operator * (const float scalar);

(matrix times integer)

Is there a way that I could multiply an int by a matrix, as opposed to
only a matrix by an int?

You could make it a non-member.

But I'm curious as to what it would do...? I know that the result of
multiplying a matrix by a scaler is another matrix, but what would the
result of multiplying the other way around be?

If the result you want is a matrix (which is the only thing that makes sense
to me), then why do you need a specific order? Can't you just re-order the
call?

Oh well, in any case, the answer is to make it a non-member, and pass both
the integer scaler (lhs) and the matrix (rhs) as parameters.

-Howard
 
K

Kai-Uwe Bux

Howard wrote:

But I'm curious as to what it would do...? I know that the result of
multiplying a matrix by a scaler is another matrix, but what would the
result of multiplying the other way around be?

Actually, by convention, the scalar *should* be on the left, i.e, it
*should* read cA, where c is a scalar and A is a matrix. The result would
be a matrix. However, since fields are commutative, there is no real
difference between a left- and a right-vector space. In other words, you
can put the scalar on either side of the matrix, the product is always just
a matrix.
If the result you want is a matrix (which is the only thing that makes
sense to me),

It's not just you :)
then why do you need a specific order? Can't you just re-order
the call?

Sounds fine.
Oh well, in any case, the answer is to make it a non-member, and pass both
the integer scaler (lhs) and the matrix (rhs) as parameters.


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,190
Latest member
ClayE7480

Latest Threads

Top