operator ++ overload - How To

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JohnGoogle@hotmail.co.uk

    operator ++ overload - How To

    Hi all,

    For my own educational process I am implementing a Fraction class
    described in the VSJ magazine to help me understand Visual C# operator
    overloading. I'm a relative newbie.

    At the moment my basic code is:


    using System;
    using System.Collecti ons.Generic;
    using System.Text;

    namespace XYZ.Common
    {

    //
    // This class is to demonstrate how to overload operators such as
    +, -, ++.
    //

    public class Fraction
    {
    private Int32 numerator;
    private Int32 denominator;

    // Constructors

    // 'Whole numbers'

    public Fraction(Int32 numerator)
    {
    this.numerator = numerator;
    this.denominato r = 1;
    }

    // 'True fractions'

    public Fraction(Int32 numerator, Int32 denominator)
    {
    this.numerator = numerator;
    this.denominato r = denominator;
    }

    // Implementation of the + operator

    public static Fraction operator+(Fract ion lhs, Fraction rhs)
    {
    if (lhs.denominato r == rhs.denominator )
    {
    return new Fraction(lhs.nu merator + rhs.numerator,
    lhs.denominator );
    }
    else
    {
    Int32 denominator = lhs.denominator * rhs.denominator ;
    Int32 firstProduct = (denominator / lhs.denominator ) *
    lhs.numerator;
    Int32 secondProduct = (denominator / rhs.denominator ) *
    rhs.numerator;

    return new Fraction(firstP roduct + secondProduct,
    denominator);
    }
    }

    // Implementation of the ++ operator - VERSION 1

    public static Fraction operator ++(Fraction f)
    {
    f.numerator++;
    return f;
    }

    // Implementation of the ++ operator - VERSION 2

    public static Fraction operator ++(Fraction f)
    {
    return new Fraction(f.nume rator + 1, f.denominator);
    }

    public override string ToString()
    {
    return numerator.ToStr ing() + "/" + denominator.ToS tring();
    }

    }
    }


    My problem is that both versions of the operator ++ method compile and
    return the correct result.

    Version 1 simply returns the original object after amending it. Version
    2 creates and returns a totally new object.


    Which is the correct version to use with regard to memory allocation
    etc?

    Thanks for any help.

  • Paul E Collins

    #2
    Re: operator ++ overload - How To

    JohnGoogle@hotm ail.co.uk wrote:
    // Implementation of the ++ operator - VERSION 1
    public static Fraction operator ++(Fraction f)
    {
    f.numerator++;
    return f;
    }
    // Implementation of the ++ operator - VERSION 2
    public static Fraction operator ++(Fraction f)
    {
    return new Fraction(f.nume rator + 1, f.denominator);
    }
    As you've discovered, they both work correctly.

    I'd strongly favour version 1, because (i) it doesn't have the
    overhead (however small) of creating a new object and performing that
    constructor call, and (ii) in the event that you add more fields to
    the class, version 1 won't reset the other fields like version 2
    would.

    Memory management isn't an issue when you're only dealing with simple
    Int32 variables.

    Eq.


    Comment

    • Jon Skeet [C# MVP]

      #3
      Re: operator ++ overload - How To

      Paul E Collins <find_my_real_a ddress@CL4.orgw rote:
      JohnGoogle@hotm ail.co.uk wrote:
      >
      // Implementation of the ++ operator - VERSION 1
      public static Fraction operator ++(Fraction f)
      {
      f.numerator++;
      return f;
      }
      // Implementation of the ++ operator - VERSION 2
      public static Fraction operator ++(Fraction f)
      {
      return new Fraction(f.nume rator + 1, f.denominator);
      }
      >
      As you've discovered, they both work correctly.
      >
      I'd strongly favour version 1, because (i) it doesn't have the
      overhead (however small) of creating a new object and performing that
      constructor call, and (ii) in the event that you add more fields to
      the class, version 1 won't reset the other fields like version 2
      would.
      But the fact that it doesn't create a new object is the problem with it
      as well. It means that after:

      Fraction f = ....;
      Fraction g = f;

      then

      f++;

      isn't the same as:

      f = f+1;

      Now, quite possibly the class should actually be a struct, but if it
      *does* need to be a class, I'd favour making it an immutable one, like
      string.

      --
      Jon Skeet - <skeet@pobox.co m>
      http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
      If replying to the group, please do not mail me too

      Comment

      • Ben Newsam

        #4
        Re: operator ++ overload - How To

        On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
        <skeet@pobox.co mwrote:
        >But the fact that it doesn't create a new object is the problem with it
        >as well. It means that after:
        >
        >Fraction f = ....;
        >Fraction g = f;
        >
        >then
        >
        >f++;
        >
        >isn't the same as:
        >
        >f = f+1;
        Correct me if I'm wrong, but surely to add 1 to a fraction, you should
        add the denominator into the numerator, not merely increment the
        numerator? If your ++ operator did that, then f++ and f = f + 1 would
        be the same.

        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: operator ++ overload - How To

          Ben Newsam <ben.newsam@uko nline.co.ukwrot e:
          On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
          <skeet@pobox.co mwrote:
          >
          But the fact that it doesn't create a new object is the problem with it
          as well. It means that after:

          Fraction f = ....;
          Fraction g = f;

          then

          f++;

          isn't the same as:

          f = f+1;
          >
          Correct me if I'm wrong, but surely to add 1 to a fraction, you should
          add the denominator into the numerator, not merely increment the
          numerator? If your ++ operator did that, then f++ and f = f + 1 would
          be the same.
          Leaving the actual addition to one side, the difference is that the
          addition operator given returns a *new* object, leaving the old one
          with the previous value - whereas the ++ operator given *changes* the
          existing value.

          But you're quite right - the ++ implementation should increment the
          numerator by the denominator, not by one. At least, I'd be surprised by
          the results of the current implementation.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          • JohnGoogle@hotmail.co.uk

            #6
            Re: operator ++ overload - How To

            Quite right about adding the denominator. Thanks. I didn't think it
            through!

            I think I'll go with version 1.

            John.


            Jon wrote:
            Ben Newsam <ben.newsam@uko nline.co.ukwrot e:
            On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
            <skeet@pobox.co mwrote:
            >But the fact that it doesn't create a new object is the problem with it
            >as well. It means that after:
            >
            >Fraction f = ....;
            >Fraction g = f;
            >
            >then
            >
            >f++;
            >
            >isn't the same as:
            >
            >f = f+1;
            Correct me if I'm wrong, but surely to add 1 to a fraction, you should
            add the denominator into the numerator, not merely increment the
            numerator? If your ++ operator did that, then f++ and f = f + 1 would
            be the same.
            >
            Leaving the actual addition to one side, the difference is that the
            addition operator given returns a *new* object, leaving the old one
            with the previous value - whereas the ++ operator given *changes* the
            existing value.
            >
            But you're quite right - the ++ implementation should increment the
            numerator by the denominator, not by one. At least, I'd be surprised by
            the results of the current implementation.
            >
            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • JohnGoogle@hotmail.co.uk

              #7
              Re: operator ++ overload - How To

              Quite right about adding the denominator. Thanks. I didn't think it
              through!

              I think I'll go with version 1.

              John.


              Jon wrote:
              Ben Newsam <ben.newsam@uko nline.co.ukwrot e:
              On Sun, 22 Oct 2006 07:19:41 +0100, Jon Skeet [C# MVP]
              <skeet@pobox.co mwrote:
              >But the fact that it doesn't create a new object is the problem with it
              >as well. It means that after:
              >
              >Fraction f = ....;
              >Fraction g = f;
              >
              >then
              >
              >f++;
              >
              >isn't the same as:
              >
              >f = f+1;
              Correct me if I'm wrong, but surely to add 1 to a fraction, you should
              add the denominator into the numerator, not merely increment the
              numerator? If your ++ operator did that, then f++ and f = f + 1 would
              be the same.
              >
              Leaving the actual addition to one side, the difference is that the
              addition operator given returns a *new* object, leaving the old one
              with the previous value - whereas the ++ operator given *changes* the
              existing value.
              >
              But you're quite right - the ++ implementation should increment the
              numerator by the denominator, not by one. At least, I'd be surprised by
              the results of the current implementation.
              >
              --
              Jon Skeet - <skeet@pobox.co m>
              http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
              If replying to the group, please do not mail me too

              Comment

              Working...