By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,847 Members | 2,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,847 IT Pros & Developers. It's quick & easy.

A question about operator*=

P: n/a
I have a matrix class that I want to add this method to.

basically I want to multiply a matrix by a matrix or by a constant.
However, I don't want to return a matrix (refrence) from the method
as this might be a performance issue.

If one implements the method, then I suppose one is not 'required'
to return the result. However that doesn't sound like 'good' programming
style as others may not realise I've done this.

Consider these two statements:

1) a *= 2;
2) b = a *= 2;

Statement 1 doesn't make use of the return result of the *= operator, where
as
statement 2 does.

So is statement 1 faster than statement 2?

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
JustSomeGuy wrote:
I have a matrix class that I want to add this method to.

basically I want to multiply a matrix by a matrix or by a constant.
However, I don't want to return a matrix (refrence) from the method
as this might be a performance issue.
Returning a matrix can be an expensive operation, just returning a
reference has minimal impact on the performance.
If one implements the method, then I suppose one is not 'required'
to return the result. However that doesn't sound like 'good' programming
style as others may not realise I've done this.
If you're afraid confusion might result, perhaps it's a better idea not
to use operator*=, but use a normal member function?
Consider these two statements:

1) a *= 2;
2) b = a *= 2;

Statement 1 doesn't make use of the return result of the *= operator, where
as
statement 2 does.

So is statement 1 faster than statement 2?


Of course, since it does less: the assigmnent to b doesn't happen. It
would be fairer to compare

a *= 2; b = a;
and
b = a *= 2;

I think there won't be much difference in performance. The first one is
easier to read though, in my opinion.

--
"Codito ergo sum"
Roel Schroeven
Jul 19 '05 #2

P: n/a

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:hY***********************@news3.calgary.shaw. ca...
I have a matrix class that I want to add this method to.

basically I want to multiply a matrix by a matrix or by a constant.
However, I don't want to return a matrix (refrence) from the method
as this might be a performance issue.


There's nothing inefficient about returning a reference. Returning a
reference from *= is normal.

john

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.