-
July 31st, 2005, 01:20 PM
#16
Re: Why should I use '++i' instead of 'i++'?
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...
-
July 31st, 2005, 04:28 PM
#17
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
-
August 1st, 2005, 05:37 AM
#18
Re: Why should I use '++i' instead of 'i++'?
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.
-
August 1st, 2005, 06:10 PM
#19
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.
-
August 1st, 2005, 07:33 PM
#20
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
-
August 2nd, 2005, 12:28 AM
#21
Re: Why should I use '++i' instead of 'i++'?
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
August 2nd, 2005, 03:12 AM
#22
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
-
August 2nd, 2005, 12:48 PM
#23
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.
-
August 2nd, 2005, 03:51 PM
#24
Re: Why should I use '++i' instead of 'i++'?
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?)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|