A question about operator*=

Discussion in 'C++' started by JustSomeGuy, Aug 9, 2003.

  1. JustSomeGuy

    JustSomeGuy Guest

    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?
    JustSomeGuy, Aug 9, 2003
    #1
    1. Advertising

  2. 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
    Roel Schroeven, Aug 9, 2003
    #2
    1. Advertising

  3. "JustSomeGuy" <> wrote in message
    news:hYcZa.687994$...
    > 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
    John Harrison, Aug 9, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jakob Bieling

    Q: operator void* or operator bool?

    Jakob Bieling, Mar 5, 2004, in forum: C++
    Replies:
    2
    Views:
    578
    Rob Williscroft
    Mar 5, 2004
  2. John Smith
    Replies:
    2
    Views:
    417
    Ivan Vecerina
    Oct 6, 2004
  3. Alex Vinokur
    Replies:
    4
    Views:
    3,045
    Peter Koch Larsen
    Nov 26, 2004
  4. Alex Vinokur
    Replies:
    3
    Views:
    5,018
    Jeff Schwab
    Mar 20, 2005
  5. Tim Clacy
    Replies:
    15
    Views:
    2,678
    Kanenas
    May 30, 2005
Loading...

Share This Page