Overloading operator++

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

    #1

    Overloading operator++

    Hi,

    I want to overload operator++ in my custom iterator class, which I do
    like this:

    internal struct IListIterator
    {
    public static IListIterator operator++(ILis tIterator ii)
    {
    idx += 1;
    // copy at the new position->prefix operator
    return new IListIterator(n ull, idx );
    }
    }

    The question is: Is this the prefix or postfix form? How do both forms
    differ in the declaration? I tried the C++ way with an second int as a
    parameter, but that of course fails as ++ is an unary operator.

    Thanks in advance,
    Andy
  • Neno Loje [MSP]

    #2
    Re: Overloading operator++

    Hi Andreas,

    in C# the ++ operator does not differentiate between the prefix and postfix
    form.

    Neno Loje


    Comment

    • Jasper Kent

      #3
      Re: Overloading operator++

      Not quite. C# requires only one overload of ++ and then works out both
      the prefix and the post fix for you. e.g.:

      struct Counter
      {
      private int i;

      public static Counter operator ++ (Counter obj)
      {
      obj.i++;

      return obj;
      }
      }

      ...

      Counter c = new Counter (); // c.i == 0

      Counter c1 = ++c; // c.i == 1, c1.i == 1

      Counter c2 = c++; // c.1 == 2, c2.i == 1

      It's more complex with classes because c, c1 and c2 would all be the
      same object, unless you returned a new object from ++, but the basic
      idea is the same.

      Regards,

      Jasper Kent

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Jasper Kent

        #4
        Re: Overloading operator++

        Here's a class version that gives the same results:

        class Counter
        {
        private int i;

        public static Counter operator ++ (Counter obj)
        {
        Counter temp = new Counter ();

        temp.i = obj.i + 1;

        return temp;
        }
        }


        Jasper Kent

        *** Sent via Developersdex http://www.developersdex.com ***
        Don't just participate in USENET...get rewarded for it!

        Comment

        • Andreas Mueller

          #5
          Re: Overloading operator++

          Jasper Kent schrieb:
          [color=blue]
          > Here's a class version that gives the same results:
          >
          > class Counter
          > {
          > private int i;
          >
          > public static Counter operator ++ (Counter obj)
          > {
          > Counter temp = new Counter ();
          >
          > temp.i = obj.i + 1;
          >
          > return temp;
          > }
          > }
          >
          >
          > Jasper Kent
          >
          > *** Sent via Developersdex http://www.developersdex.com ***
          > Don't just participate in USENET...get rewarded for it![/color]

          Great, that is doing the job!
          Thanks a lot,
          Andy

          Comment

          Working...