i++ and ++i when the ++ operator is overriden

Discussion in 'C++' started by Binary, Oct 24, 2006.

  1. Binary

    Binary Guest

    Hi,

    In this board's FAQ, the operator override section says the i++ and ++i
    has some difference, the i++ will make a copy of this pointer.
    I can't know the reason, why?

    Thanks in advance.
    ABAI
    Binary, Oct 24, 2006
    #1
    1. Advertising

  2. Binary

    Kai-Uwe Bux Guest

    Binary wrote:

    > Hi,
    >
    > In this board's FAQ, the operator override section says the i++ and ++i
    > has some difference, the i++ will make a copy of this pointer.
    > I can't know the reason, why?


    i++ is supposed to return the value of i *before* incrementing. So, if you
    implement postincrement for a class, the typical implementation looks
    somewhat like this:

    class SomeIterator {

    // ...

    SomeIterator operator++ ( int ) {
    // save the old value
    SomeIterator return_value ( *this );
    // increment
    ++ *this;
    // return the old value
    return( return_value );
    }

    };


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 24, 2006
    #2
    1. Advertising

  3. Binary

    Binary Guest

    Perfect! Thank you!

    ABAI
    Kai-Uwe Bux wrote:
    > Binary wrote:
    >
    > > Hi,
    > >
    > > In this board's FAQ, the operator override section says the i++ and ++i
    > > has some difference, the i++ will make a copy of this pointer.
    > > I can't know the reason, why?

    >
    > i++ is supposed to return the value of i *before* incrementing. So, if you
    > implement postincrement for a class, the typical implementation looks
    > somewhat like this:
    >
    > class SomeIterator {
    >
    > // ...
    >
    > SomeIterator operator++ ( int ) {
    > // save the old value
    > SomeIterator return_value ( *this );
    > // increment
    > ++ *this;
    > // return the old value
    > return( return_value );
    > }
    >
    > };
    >
    >
    > Best
    >
    > Kai-Uwe Bux
    Binary, Oct 24, 2006
    #3
  4. Binary

    Guest

    Binary wrote:
    > Hi,
    >
    > In this board's FAQ, the operator override section says the i++ and ++i
    > has some difference, the i++ will make a copy of this pointer.


    /should/ make a copy. Each built-in operator++(int) makes a copy, and
    your
    versions should behave the same. The same applies to operator==; your
    versions should return a bool. Else, people who use it would be
    surprised.

    I.e. it's not a technical requirement but a usability reason.

    HTH,
    Michiel Salters
    , Oct 24, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    51
    Views:
    4,248
    Tony Morris
    Jul 14, 2004
  2. =?Utf-8?B?Y2hlbG9tYW4xMg==?=

    overriden viewstate events and gridview

    =?Utf-8?B?Y2hlbG9tYW4xMg==?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    =?Utf-8?B?Y2hlbG9tYW4xMg==?=
    May 4, 2006
  3. =?Utf-8?B?Y2hlbG9tYW4xMg==?=

    GridView problems with viewstate events overriden.

    =?Utf-8?B?Y2hlbG9tYW4xMg==?=, May 9, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    4,510
    Teemu Keiski
    May 9, 2006
  4. Makis Papapanagiotou

    Re: return types for virtual overriden functions

    Makis Papapanagiotou, Jul 31, 2003, in forum: C++
    Replies:
    2
    Views:
    298
    Victor Bazarov
    Jul 31, 2003
  5. Bob Jones
    Replies:
    12
    Views:
    506
    Bob Jones
    Feb 28, 2007
Loading...

Share This Page