473,378 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

A question about operator*=

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
2 1506
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
36
by: utab | last post by:
Dear, I have experince in C( numerical projects, like engineering problems, scientific applications) I have the basic notion of C++ also, I have read Accelerated C++ until Chapter 7, however it...
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
1
by: Gonçalo Rodrigues | last post by:
Hi all, I am a little confused about the delete operator, so I have a question. Suppose we have something like class Base { public: void* operator new(std::size_t size); void operator...
15
by: Jeroen | last post by:
Hi all, I've got a very specific question about the evaluation order in C++. Assume some kind of custom array class, with an overloaded subscript operator. In the following code: { my_array...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
7
by: vlsidesign | last post by:
I am not sure how to think about parentheses. It seems when they are used alongside characters it indicates a function, like addTwoNums(). But is also is used to enforce which operators and...
8
by: Tony Johansson | last post by:
Hello! I think that this text is complete enough so you can give me an explanation what they mean with it. I wonder if somebody understand what this means. It says the following : "You can't...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.