CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 24 of 24
  1. #16
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Thumbs up Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by NMTop40
    I prefer to use ++i but I did come from a C background originally and i++ was far more popular (perhaps why the language is called C++ and not ++C). Perhaps because i++ is shorthand for i+=1 (at least it appears to be) where the operation on i is on the right-hand side. You cannot do 1+=i (1 is not an l-value) or even 1=+i (same error).
    Hehe, even if 'i+=1' looks like a shortland of 'i++', it is equivalent to '++i' and it is different of 'i++'.
    I hope that no C programmer wrote buggy program because of that confusion.


    ++C, it looks like a pretty language, maybe in the future...

  2. #17
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Why should I use '++i' instead of 'i++'?

    It's called C++ because it's an incremental improvement over C, but most programmers still use the original value...
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #18
    Join Date
    Jan 2001
    Posts
    588

    Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by Graham
    It's called C++ because it's an incremental improvement over C, but most programmers still use the original value...
    I think that's an excellent way to put it, Graham.

  4. #19
    Join Date
    Apr 2005
    Posts
    107

    Re: Why should I use '++i' instead of 'i++'?

    I'm sorry if I just missed it, but I didn't see the primary reason for avoiding a post-increment (x++) in C++; while for simple, intrinsic datatypes, there is usually no difference, if you are using an increment on an object, the post-increment calls a copy constructor to build a new object, where the pre-increment doesn't.

  5. #20
    Join Date
    Jun 2005
    Location
    The Netherlands
    Posts
    185

    Re: Why should I use '++i' instead of 'i++'?

    so ++i is faster for objects so its better to get used to using ++i than using i++
    also having a mix of i++ and ++obj looks bad

  6. #21
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by rdrast
    if you are using an increment on an object, the post-increment calls a copy constructor to build a new object, where the pre-increment doesn't.
    First of all, the post increment and pre increment operators (basically ++) is not by default defined with classes. So you wont be able to do that for objects as you mentioned. Secondly, it depends how they are implemented - they might as well be acting upon the intrinsic datatypes in turn, as defined by the implementer. So, who knows without having implementation in hand? So your statement is kind of not complete in my opinion.

  7. #22
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Why should I use '++i' instead of 'i++'?

    The point is that pre-increment will never be slower than post-increment and may be faster.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #23
    Join Date
    Jan 2005
    Posts
    4

    Resolved Re: Why should I use '++i' instead of 'i++'?

    In C we donot have operator overloading concept. so both r same. Now in C++ it depends on how u implement. As per general implementation if my class name is ClassEx and if I want to overload the post/pre increment operators We do as

    Pre increment (++i)
    ================
    ClassEx& operator ++()
    {
    ++(*this);
    ......
    return *this;
    }

    Post Increment (i++)
    ================
    ClassEx operator(int)
    {
    ClassEx clNew = this;
    ++(*this);
    return clNew;
    }

    So Now for preincrement we return a reference....for post increment we return an object that is created newly.....

    So ++i is faster than i++..........atleast you will not loose any thing in using ++i..........u will gain something....

    Thanks,
    Anand.

  9. #24
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Why should I use '++i' instead of 'i++'?

    Quote Originally Posted by anand_chavali
    Post Increment (i++)
    ================
    ClassEx operator(int)
    {
    ClassEx clNew = this;
    ++(*this);
    return clNew;
    }
    ClassEx clNew = this; won't work, not unless you have defined operator= like this:

    Code:
    class ClassEx
    {
    public:
       ClassEx& operator=( const ClassEx * );
    }
    which is unlikely. In any case in your implementation neither is faster since your pre-increment recurses ad-infinitum, and your post-incrementer calls it thus also recurses ad-infinitum. Well logically they take the same time as infinity plus a bit is not greater than infinity, but either way you'll run out of stack and I don't know which one will run you out of stack quicker.

    Now:

    Code:
    class ClassEx
    {
    public:
       ClassEx& operator++()
       {
            // perform the increment as appropriate
           return *this;
       }
    
       ClassEx operator++(int)
       {
           ClassEx temp( *this ); // not  = this
           operator++(); // equivalent to ++(*this)
    
           return temp;
       }
    };
    Anyway, operator++(int) makes 2 copies. The second one will probably be optimised away by the compiler but it's unlikely the first one will be even if you don't access the return value.

    As a point of interest though, when you DO want the original value and then increment, is it still better to use pre-increment (thus 2 statements). eg
    as an implementation of copy
    Code:
    template < typename FwdIt, typename OutIt >
    void copy( FwdIt begin, FwdIt end, OutIt dest )
    {
       while ( begin != end )
       {
            *dest++ = *begin++;
       }
    }
    or
    Code:
    template < typename FwdIt, typename OutIt >
    void copy( FwdIt begin, FwdIt end, OutIt dest )
    {
       while ( begin != end )
       {
            *dest = *begin;
            ++dest;
            ++begin;
       }
    }
    The second one appears to use more lines of code but given the implementation of operator++(int) perhaps it is illusionary? (If the iterators really are pointers it probably makes no difference but what if they are not pointers?)

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured