Overloading binary + operator

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

    Overloading binary + operator

    Hi all,
    I need to sum two classes in this way

    MyClass CT = myCollectionOfM yClass[0];
    MyClass C1 = myCollectionOfM yClass[1];

    CT = CT + C1;

    In myClass exists the method to overload + operator and the method
    looks like this:

    public override operator +(MyClass class1, MyClass class2)
    {
    MyClass class3 = new MyClass();
    class3.property 1 = class1.property 1 + class2.property 2;
    .......
    return class3;
    }

    If I execute this code, the value of CT.property1 is 0. Also if
    C1.property is 1.

    To obtain the correct result (1) I must to change the code in this
    way:

    public override operator +(MyClass class1, MyClass class2)
    {
    MyClass class3 = class1;
    class3.property 1 = class1.property 1 + class2.property 2;
    .......
    return class3;
    }

    It seems that when I create a new istance of MyClass in the overloaded
    method, the CT class was cleared.

    Anyone knows the correct way to do this????

    Bye
    Sapo
  • Hans Kesting

    #2
    Re: Overloading binary + operator

    It happens that Sapo19875 formulated :
    Hi all,
    I need to sum two classes in this way
    >
    MyClass CT = myCollectionOfM yClass[0];
    MyClass C1 = myCollectionOfM yClass[1];
    >
    CT = CT + C1;
    >
    In myClass exists the method to overload + operator and the method
    looks like this:
    >
    public override operator +(MyClass class1, MyClass class2)
    {
    MyClass class3 = new MyClass();
    class3.property 1 = class1.property 1 + class2.property 2;
    .......
    return class3;
    }
    >
    If I execute this code, the value of CT.property1 is 0. Also if
    C1.property is 1.
    >
    Bye
    Sapo
    How are you checking those results? The variable CT should contain a
    new reference to that (new) "class3" instance. But the value of
    myCollectionOfM yClass[0] did not change, that still points to the "old"
    CT.

    Hans Kesting


    Comment

    • Sapo19875

      #3
      Re: Overloading binary + operator

      >
      How are you checking those results? The variable CT should contain a
      new reference to that (new) "class3" instance. But the value of
      myCollectionOfM yClass[0] did not change, that still points to the "old"
      CT.
      >
      Hans Kesting
      thanks Hans for your quickly reply.
      the problem is right there. I'd like to change the property of the
      class in the collecion.

      I'll change the code replacing the "new istance " whith de "old
      istance" like method 2....

      Thanks again
      Sapo

      Comment

      • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

        #4
        Re: Overloading binary + operator

        Sapo19875 wrote:
        >How are you checking those results? The variable CT should contain a
        >new reference to that (new) "class3" instance. But the value of
        >myCollectionOf MyClass[0] did not change, that still points to the "old"
        >CT.
        >>
        >Hans Kesting
        >
        thanks Hans for your quickly reply.
        the problem is right there. I'd like to change the property of the
        class in the collecion.
        >
        I'll change the code replacing the "new istance " whith de "old
        istance" like method 2....
        >
        Thanks again
        Sapo
        You had the right implementation to begin with:

        public override operator +(MyClass class1, MyClass class2)
        {
        MyClass class3 = new MyClass();
        class3.property 1 = class1.property 1 + class2.property 2;
        .......
        return class3;
        }

        This will produce a new MyClass instance that contains the result of
        adding class1 and class2.

        --
        Lasse Vågsæther Karlsen
        mailto:lasse@vk arlsen.no
        Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

        PGP KeyID: 0xBCDEA2E3

        Comment

        • Sapo19875

          #5
          Re: Overloading binary + operator

          >
          You had the right implementation to begin with:
          >
          public override operator +(MyClass class1, MyClass class2)
          {
               MyClass class3 = new MyClass();
               class3.property 1 = class1.property 1 + class2.property 2;
               .......
               return class3;
          >
          }
          >
          This will produce a new MyClass instance that contains the result of
          adding class1 and class2.
          >
          --
          Lasse Vågsæther Karlsen
          mailto:la...@vk arlsen.nohttp://presentationmod e.blogspot.com/
          PGP KeyID: 0xBCDEA2E3- Nascondi testo tra virgolette -
          >
          - Mostra testo tra virgolette -
          Ok Lasse, my goal was to change the vaule of the property stored in
          the collection.
          I know that the implementation of my method was good (it's the same of
          the c# guide....eheh) but,
          as i've wrote, when my code return to te caller, the property in the
          collection doesn't change...

          Bye

          Comment

          • Rene

            #6
            Re: Overloading binary + operator

            Well, I realize that I am not answering your question but I could not help
            noticing the:

            public override operator +(MyClass class1, MyClass class2)

            as far as I understand this should be something like this:

            public static MyClass operator +( MyClass class1, MyClass class2)

            Chances are you where typing your post on the fly and you missed the correct
            syntax but in case you didn't and that is how your code really looks like I
            would like to know because I am kind of curious.

            The other thing to mention is that in your sample code your are adding
            proerty1 + proerty2, shouldn't that be property1 + property2?



            "Sapo19875" <samuele.mori1@ virgilio.itwrot e in message
            news:90b9eab3-484f-4499-a0dc-345ad96cf5c1@x4 1g2000hsb.googl egroups.com...
            Hi all,
            I need to sum two classes in this way
            >
            MyClass CT = myCollectionOfM yClass[0];
            MyClass C1 = myCollectionOfM yClass[1];
            >
            CT = CT + C1;
            >
            In myClass exists the method to overload + operator and the method
            looks like this:
            >
            public override operator +(MyClass class1, MyClass class2)
            {
            MyClass class3 = new MyClass();
            class3.property 1 = class1.property 1 + class2.property 2;
            .......
            return class3;
            }
            >
            If I execute this code, the value of CT.property1 is 0. Also if
            C1.property is 1.
            >
            To obtain the correct result (1) I must to change the code in this
            way:
            >
            public override operator +(MyClass class1, MyClass class2)
            {
            MyClass class3 = class1;
            class3.property 1 = class1.property 1 + class2.property 2;
            .......
            return class3;
            }
            >
            It seems that when I create a new istance of MyClass in the overloaded
            method, the CT class was cleared.
            >
            Anyone knows the correct way to do this????
            >
            Bye
            Sapo

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Overloading binary + operator

              On Apr 15, 3:58 am, Sapo19875 <samuele.mo...@ virgilio.itwrot e:

              <snip>
              public override operator +(MyClass class1, MyClass class2)
               {
                       MyClass class3 = class1;
                       class3.property 1 = class1.property 1 + class2.property 2;
                       .......
                       return class3;
               }
              You're changing the original value here (assuming it's genuinely a
              class). You should be creating a *new* instance of MyClass, not just
              changing the existing instance. The line:

              MyClass class3 = class1;

              should be something to create a new instance of MyClass from class1,
              e.g.

              MyClass class3 = new MyClass(class1) ;

              where you define the appropriate constructor.

              If that doesn't help, please post a short but complete program
              demonstrating the problem. This clearly isn't your real code, and we
              can't tell which mistakes (e.g. using property2 instead of property1)
              are in your real code and which aren't. A complete program which we
              can compile and run will be easier to fix.

              Jon

              Comment

              • =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

                #8
                Re: Overloading binary + operator

                Sapo19875 wrote:
                >You had the right implementation to begin with:
                >>
                >public override operator +(MyClass class1, MyClass class2)
                >{
                > MyClass class3 = new MyClass();
                > class3.property 1 = class1.property 1 + class2.property 2;
                > .......
                > return class3;
                >>
                >}
                >>
                >This will produce a new MyClass instance that contains the result of
                >adding class1 and class2.
                >>
                >--
                >Lasse Vågsæther Karlsen
                >mailto:la...@v karlsen.nohttp://presentationmod e.blogspot.com/
                >PGP KeyID: 0xBCDEA2E3- Nascondi testo tra virgolette -
                >>
                >- Mostra testo tra virgolette -
                >
                Ok Lasse, my goal was to change the vaule of the property stored in
                the collection.
                I know that the implementation of my method was good (it's the same of
                the c# guide....eheh) but,
                as i've wrote, when my code return to te caller, the property in the
                collection doesn't change...
                >
                Bye
                That is because you're not modifying the collection at all.

                Your original + operator takes two objects, from the collection or
                otherwise, and produce a new, third, object with the results. It does
                not, and should not, modify any of the original two objects passed to it.

                As such, the way to get your collection to be modified is to modify the
                collection:

                myCollectionCla ss[0] = myCollectionCla ss[0] + myCollectionCla ss[1];

                or just:

                myCollectionCla ss[0] += myCollectionCla ss[1];

                note that this will replace the original object at element 0 with the
                new object that was the result of the addition. The original object,
                unless referenced somewhere else, will just be left for the garbage
                collector to handle sometime later.

                If you actually want to update the object at element 0 instead of
                replacing it, the + operator is not what you want, instead add methods
                on the object to accumulate the results of adding another object to
                itself, ie.

                public void Accumulate(MyCl ass other)
                {
                property1 += other.property1 ;
                ....
                }

                and then call that instead:

                myCollectionCla ss[0].Accumulate(myC ollectionClass[1]);

                --
                Lasse Vågsæther Karlsen
                mailto:lasse@vk arlsen.no
                Blogger ist ein Veröffentlichungs-Tool von Google, mit dem du ganz einfach deine Gedanken der Welt mitteilen kannst. Mit Blogger kannst du problemlos Texte, Fotos und Videos in deinem persönlichen Blog oder deinem Team-Blog veröffentlichen.

                PGP KeyID: 0xBCDEA2E3

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Overloading binary + operator

                  On Apr 15, 6:38 am, Sapo19875 <samuele.mo...@ virgilio.itwrot e:

                  <snip>
                  Ok Lasse, my goal was to change the vaule of the property stored in
                  the collection.
                  In that case you need to change the collection, e.g.

                  collection[0] += collection[1];
                  which is shorthand for
                  collection[0] = collection[0] + collection[1];

                  That will replace the current element 0 in the collection with the sum
                  of element 0 and element 1.

                  The + operator should not change its operands, it should return a new
                  value. i.e.

                  x = y + z;

                  shouldn't change the value of y or z.

                  Jon

                  Comment

                  Working...