How To overload =

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Harry J. Smith

    How To overload =

    How do you overload the = operator?

    public static MultiUI operator = (MultiUI left, MultiUI right)

    {

    return right;

    }

    gives an error of "overloadab le binary operator expected"



    -Harry


  • cody

    #2
    Re: How To overload =

    You cannot overload operator=, but you can overload the type conversion
    operator.
    According to your example what sense would it make to overload operator=
    which only assign one objects of type T to another object with exact the
    same type?

    --
    cody

    [Freeware, Games and Humor]
    www.deutronium.de.vu || www.deutronium.tk
    "Harry J. Smith" <hjsmith@ix.net com.com> schrieb im Newsbeitrag
    news:A6%Jc.5650 $Qu5.4543@newsr ead2.news.pas.e arthlink.net...[color=blue]
    > How do you overload the = operator?
    >
    > public static MultiUI operator = (MultiUI left, MultiUI right)
    >
    > {
    >
    > return right;
    >
    > }
    >
    > gives an error of "overloadab le binary operator expected"
    >
    >
    >
    > -Harry
    >
    >[/color]


    Comment

    • Daniel Billingsley

      #3
      Re: How To overload =

      Yeah, what he said. :)

      I thought I needed to overload = to create my own string-like class until I
      read this article where I learned overloading the type conversion did the
      trick.



      "cody" <please_dont.sp am.deutronium@g mx.de> wrote in message
      news:ei1%23Vw6a EHA.2816@TK2MSF TNGP11.phx.gbl. ..[color=blue]
      > You cannot overload operator=, but you can overload the type conversion
      > operator.
      > According to your example what sense would it make to overload operator=
      > which only assign one objects of type T to another object with exact the
      > same type?
      >[/color]


      Comment

      • Harry J. Smith

        #4
        Re: How To overload =

        When you do a = b, where a and b are two objects, only the pointer to b is copied so a and b are the same object. The disassembly
        shows:

        a = b;

        000000d7 mov edi,dword ptr [ebp-18h]

        I wanted a function to copy all of the fields of b to a and have two objects whose fields are currently equal. I can do this with a
        function like SetTo(b, a), but I wanted to do it with the = operator. This may be a bad idea because if you could do it you would
        loose the copy pointer method.

        -Harry

        "cody" <please_dont.sp am.deutronium@g mx.de> wrote in message news:ei1%23Vw6a EHA.2816@TK2MSF TNGP11.phx.gbl. ..[color=blue]
        > You cannot overload operator=, but you can overload the type conversion
        > operator.
        > According to your example what sense would it make to overload operator=
        > which only assign one objects of type T to another object with exact the
        > same type?
        >
        > --
        > cody
        >
        > [Freeware, Games and Humor]
        > www.deutronium.de.vu || www.deutronium.tk
        > "Harry J. Smith" <hjsmith@ix.net com.com> schrieb im Newsbeitrag
        > news:A6%Jc.5650 $Qu5.4543@newsr ead2.news.pas.e arthlink.net...[color=green]
        > > How do you overload the = operator?
        > >
        > > public static MultiUI operator = (MultiUI left, MultiUI right)
        > >
        > > {
        > >
        > > return right;
        > >
        > > }
        > >
        > > gives an error of "overloadab le binary operator expected"
        > >
        > >
        > >
        > > -Harry
        > >
        > >[/color]
        >
        >[/color]



        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: How To overload =

          Harry J. Smith <hjsmith@ix.net com.com> wrote:[color=blue]
          > When you do a = b, where a and b are two objects, only the pointer to
          > b is copied so a and b are the same object. The disassembly
          > shows:
          >
          > a = b;
          >
          > 000000d7 mov edi,dword ptr [ebp-18h]
          >
          > I wanted a function to copy all of the fields of b to a and have two
          > objects whose fields are currently equal. I can do this with a
          > function like SetTo(b, a), but I wanted to do it with the = operator.
          > This may be a bad idea because if you could do it you would
          > loose the copy pointer method.[/color]

          So you want value type semantics for assignment. Do you need reference
          type semantics elsewhere, or could you just make your type a reference
          type?

          Don't forget that assignments effectively happen elsewhere - what would
          you want to happen when someone passed a parameter? Should that make a
          new object or not?

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Stu Smith

            #6
            Re: How To overload =


            "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
            news:MPG.1b6349 3faf3b7bca98aef a@msnews.micros oft.com...[color=blue]
            > Harry J. Smith <hjsmith@ix.net com.com> wrote:[color=green]
            > > When you do a = b, where a and b are two objects, only the pointer to
            > > b is copied so a and b are the same object. The disassembly
            > > shows:
            > >
            > > a = b;
            > >
            > > 000000d7 mov edi,dword ptr [ebp-18h]
            > >
            > > I wanted a function to copy all of the fields of b to a and have two
            > > objects whose fields are currently equal. I can do this with a
            > > function like SetTo(b, a), but I wanted to do it with the = operator.
            > > This may be a bad idea because if you could do it you would
            > > loose the copy pointer method.[/color]
            >
            > So you want value type semantics for assignment. Do you need reference
            > type semantics elsewhere, or could you just make your type a reference
            > type?[/color]

            Didn't you mean to say "...or could you just make your type a _value_ type?"
            [color=blue]
            >
            > Don't forget that assignments effectively happen elsewhere - what would
            > you want to happen when someone passed a parameter? Should that make a
            > new object or not?
            >
            > --
            > Jon Skeet - <skeet@pobox.co m>
            > http://www.pobox.com/~skeet
            > If replying to the group, please do not mail me too[/color]


            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: How To overload =

              Stu Smith <stuarts@nosp am-digita.com> wrote:[color=blue][color=green]
              > > So you want value type semantics for assignment. Do you need reference
              > > type semantics elsewhere, or could you just make your type a reference
              > > type?[/color]
              >
              > Didn't you mean to say "...or could you just make your type a _value_ type?"[/color]

              Yup. Oops.

              --
              Jon Skeet - <skeet@pobox.co m>
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

              If replying to the group, please do not mail me too

              Comment

              Working...