A question about operator*=

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JustSomeGuy

    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?



  • Roel Schroeven

    #2
    Re: A question about operator*=

    JustSomeGuy wrote:[color=blue]
    > 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.[/color]

    Returning a matrix can be an expensive operation, just returning a
    reference has minimal impact on the performance.[color=blue]
    >
    > 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.[/color]

    If you're afraid confusion might result, perhaps it's a better idea not
    to use operator*=, but use a normal member function?
    [color=blue]
    > 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?[/color]

    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

    Comment

    • John Harrison

      #3
      Re: A question about operator*=


      "JustSomeGu y" <nope@nottellin g.com> wrote in message
      news:hYcZa.6879 94$3C2.16112623 @news3.calgary. shaw.ca...[color=blue]
      > 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.
      >[/color]

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

      john



      Comment

      Working...