how to write an operator

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

    #1

    how to write an operator

    which is preferable and WHY?

    class Complex {
    ....
    friend Complex operator + (const Complex& lhs, const Complex& rhs);

    OR

    friend const Complex operator + (const Complex& lhs, const Complex& rhs);

    OR either one but a member operator instead of a friend.

    Thanx,
    Martin


  • Phlip

    #2
    Re: how to write an operator

    Martin Vorbrodt wrote:
    [color=blue]
    > which is preferable and WHY?
    >
    > class Complex {
    > ...
    > friend Complex operator + (const Complex& lhs, const Complex& rhs);
    >
    > OR
    >
    > friend const Complex operator + (const Complex& lhs, const Complex& rhs);[/color]
    ^
    That const adds no value, and there are those who claim it interferes with
    certain type-specific operations.

    If you meant 'Complex const &', don't do that either, because functions
    generally should not return value-types by reference.
    [color=blue]
    > OR either one but a member operator instead of a friend.[/color]

    A member operator might not balance properly:

    5 + Complex(8, 2);

    --
    Phlip



    Comment

    • DaKoadMunky

      #3
      Re: how to write an operator

      >That const adds no value

      Well, if you believe what some people say about operator overloading and "do as
      the ints do" then it does make it behave more like a function that returns a
      built-in type by value.

      int a,b,c;
      (a+b) = c; //This should not compile

      Complex a,b,c;
      (a+b) = c; //This will compile if return value is not const.

      For reasons I have never grasped a user-defined type returned by value is not
      an lvalue yet it can be the target of modifying operations. To make the
      overloaded operator behave in a way analagous to the built-in operators you
      must make the return value const.

      Does this have value?

      I don't know.


      Comment

      • Siemel Naran

        #4
        Re: how to write an operator

        "DaKoadMunk y" <dakoadmunky@ao l.com> wrote in message[color=blue]
        > Phlip[/color]
        [color=blue][color=green][color=darkred]
        > > > friend const Complex operator + (const Complex& lhs, const Complex&[/color][/color][/color]
        rhs);
        [color=blue][color=green]
        > >That const adds no value[/color][/color]
        [color=blue]
        > Well, if you believe what some people say about operator overloading and[/color]
        "do as[color=blue]
        > the ints do" then it does make it behave more like a function that returns[/color]
        a[color=blue]
        > built-in type by value.
        >
        > int a,b,c;
        > (a+b) = c; //This should not compile
        >
        > Complex a,b,c;
        > (a+b) = c; //This will compile if return value is not const.
        >
        > For reasons I have never grasped a user-defined type returned by value is[/color]
        not[color=blue]
        > an lvalue yet it can be the target of modifying operations. To make the
        > overloaded operator behave in a way analagous to the built-in operators[/color]
        you[color=blue]
        > must make the return value const.
        >
        > Does this have value?
        >
        > I don't know.[/color]

        Yes, it prevents accidentally modifying a newly returned object, so might
        catch code which compiles but may do the wrong thing.

        a++ = b;


        Comment

        • Peter Koch Larsen

          #5
          Re: how to write an operator


          "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> skrev i en meddelelse
          news:cgqr01$567 $1@news.onet.pl ...[color=blue]
          > which is preferable and WHY?
          >
          > class Complex {
          > ...
          > friend Complex operator + (const Complex& lhs, const Complex& rhs);
          >
          > OR
          >
          > friend const Complex operator + (const Complex& lhs, const Complex& rhs);
          >
          > OR either one but a member operator instead of a friend.
          >
          > Thanx,
          > Martin
          >
          >[/color]
          You should prefer

          class Complex
          {
          Complex& operator+=(Comp lex const& rhs);
          ......

          then you can have:
          Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
          rhs; }


          Comment

          • Martin Vorbrodt

            #6
            Re: how to write an operator

            So how about +=, etc operators.
            I assume they return a reference, not a constant referance,
            since this code is legal and works just fine:

            int a = 1, b = 2, c = 3;
            a += b += c;
            (a += b) += c;
            a += (b += c);


            "DaKoadMunk y" <dakoadmunky@ao l.com> wrote in message
            news:2004082821 3050.19087.0000 4285@mb-m06.aol.com...[color=blue][color=green]
            > >That const adds no value[/color]
            >
            > Well, if you believe what some people say about operator overloading and[/color]
            "do as[color=blue]
            > the ints do" then it does make it behave more like a function that returns[/color]
            a[color=blue]
            > built-in type by value.
            >
            > int a,b,c;
            > (a+b) = c; //This should not compile
            >
            > Complex a,b,c;
            > (a+b) = c; //This will compile if return value is not const.
            >
            > For reasons I have never grasped a user-defined type returned by value is[/color]
            not[color=blue]
            > an lvalue yet it can be the target of modifying operations. To make the
            > overloaded operator behave in a way analagous to the built-in operators[/color]
            you[color=blue]
            > must make the return value const.
            >
            > Does this have value?
            >
            > I don't know.
            >
            >[/color]


            Comment

            • Martin Vorbrodt

              #7
              Re: how to write an operator

              Why would i want to do that? That would modify LHS variable. I don't want
              that.


              "Peter Koch Larsen" <pklspam@mailme .dk> wrote in message
              news:pglYc.4161 9$Vf.2198766@ne ws000.worldonli ne.dk...[color=blue]
              >
              > "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> skrev i en meddelelse
              > news:cgqr01$567 $1@news.onet.pl ...[color=green]
              > > which is preferable and WHY?
              > >
              > > class Complex {
              > > ...
              > > friend Complex operator + (const Complex& lhs, const Complex& rhs);
              > >
              > > OR
              > >
              > > friend const Complex operator + (const Complex& lhs, const Complex&[/color][/color]
              rhs);[color=blue][color=green]
              > >
              > > OR either one but a member operator instead of a friend.
              > >
              > > Thanx,
              > > Martin
              > >
              > >[/color]
              > You should prefer
              >
              > class Complex
              > {
              > Complex& operator+=(Comp lex const& rhs);
              > .....
              >
              > then you can have:
              > Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
              > rhs; }
              >
              >[/color]


              Comment

              • Siemel Naran

                #8
                Re: how to write an operator

                "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> wrote in message news:cgsrv4[color=blue]
                > "Peter Koch Larsen" <pklspam@mailme .dk> wrote in message[/color]
                [color=blue][color=green]
                > > You should prefer
                > >
                > > class Complex
                > > {
                > > Complex& operator+=(Comp lex const& rhs);
                > > .....
                > >
                > > then you can have:
                > > Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
                > > rhs; }[/color][/color]

                That's assuming we want both operators, which is a reasonable assumption.
                [color=blue]
                > Why would i want to do that? That would modify LHS variable. I don't want
                > that.[/color]

                It's fine. Look closely at the function arguments. The function receives
                lhs by value, so it's a copy. However, it should be declared not const.
                This should suffice.

                Complex operator +(Complex lhs,Complex const& rhs) { return lhs += rhs; }

                You can optionally return a const Complex, as indicated in the other
                sub-thread to prevent accidental assignment to the returned unnamed
                temporary.

                const Complex operator +(Complex lhs,Complex const& rhs) { return lhs +=
                rhs; }


                Comment

                • Siemel Naran

                  #9
                  Re: how to write an operator

                  > "DaKoadMunk y" <dakoadmunky@ao l.com> wrote in message[color=blue][color=green]
                  > > "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> wrote in message[/color][/color]
                  news:cgsrtm$nd9

                  We prefer if you reply to posts in place, that is include the quoted text
                  then your comments to that quoted text, then more quoted text. This makes
                  it easier for people to read. At work of course, I usually just hit the
                  reply button as it's faster.
                  [color=blue][color=green]
                  > > int a,b,c;
                  > > (a+b) = c; //This should not compile[/color][/color]
                  [color=blue]
                  > So how about +=, etc operators.
                  > I assume they return a reference, not a constant referance,
                  > since this code is legal and works just fine:
                  >
                  > int a = 1, b = 2, c = 3;
                  > a += b += c;
                  > (a += b) += c;
                  > a += (b += c);[/color]

                  In return a const object, we're talking about returned a value. Of course,
                  returning a reference to an object is another story. We have to return a
                  non-const or const reference as appropriate to the design. As for returning
                  an object by value, as with operator++ operator+, it doesn't really matter
                  whether we return the object as const or not, but returning const is a
                  little safer and could catch strange bugs.


                  Comment

                  • Peter Koch Larsen

                    #10
                    Re: how to write an operator


                    "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> skrev i en meddelelse
                    news:cgsrv4$qam $1@news.onet.pl ...[color=blue]
                    > Why would i want to do that? That would modify LHS variable. I don't want
                    > that.[/color]

                    Because you almost certainly wants the operator+=. For users of your class
                    it will be confusing if they can write a = a + b but not a += b.

                    As you can see from my code, this makes operator+ very easy to write - and
                    it does not have to be a friend function.

                    Also i must add that i find your reply quite confusing. Do not toppost.

                    Kind regards
                    Peter
                    [color=blue]
                    >
                    >
                    > "Peter Koch Larsen" <pklspam@mailme .dk> wrote in message
                    > news:pglYc.4161 9$Vf.2198766@ne ws000.worldonli ne.dk...[color=green]
                    > >
                    > > "Martin Vorbrodt" <mvorbrodt@pocz ta.onet.pl> skrev i en meddelelse
                    > > news:cgqr01$567 $1@news.onet.pl ...[color=darkred]
                    > > > which is preferable and WHY?
                    > > >
                    > > > class Complex {
                    > > > ...
                    > > > friend Complex operator + (const Complex& lhs, const Complex& rhs);
                    > > >
                    > > > OR
                    > > >
                    > > > friend const Complex operator + (const Complex& lhs, const Complex&[/color][/color]
                    > rhs);[color=green][color=darkred]
                    > > >
                    > > > OR either one but a member operator instead of a friend.
                    > > >
                    > > > Thanx,
                    > > > Martin
                    > > >
                    > > >[/color]
                    > > You should prefer
                    > >
                    > > class Complex
                    > > {
                    > > Complex& operator+=(Comp lex const& rhs);
                    > > .....
                    > >
                    > > then you can have:
                    > > Complex operator +(const Complex lhs,Complex const& rhs) { return lhs +=
                    > > rhs; }
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    Working...