-
February 13th, 2003, 01:18 PM
#1
C++ Operator: Why should I use '++i' instead of 'i++'?
Q: Someone told me that if I want to increment a variable and that's all, I should use ++x instead of x++. Is this true?
A: If incrementing is all that you want to do (you are not assigning the variable to something else), then yes this is a good practice, especially if you are using the increment operator on a class.
Code:
// Prefix operators are preferred in cases in the following common cases:
for (;checkstate(x);++x) dosomething(x);
++x;
For the standard data types there is usually no performance difference, but for classes there are! The reason is that (for most common implementations of) postfix operators retain a temporary copy of original variable and because the return value is returned by value, not by reference. The following code exemplifies this:
Code:
class MyClass
{
public:
MyClass& operator++() //Prefix increment operator (++x)
{
//Perform increment operation
return *this;
}
MyClass operator++(int) //Postfix increment operator (x++)
{
MyClass temp = *this;
//Perform increment operation
return temp;
}
};
FAQ contributed by: [Kevin Hall]
Last edited by Andreas Masur; July 24th, 2005 at 04:37 AM.
-
June 30th, 2005, 11:31 AM
#2
Re: C++ Operator: Why should I use '++i' instead of 'i++'?
Q: So what form of increment/decrement operator should I use for fundamental types when I can use either. For example:
Code:
double d = 3.14;
d--; // or should I use --d?
for (int i=0; i<10; i++) [COLOR=#008800]// or should I use ++i?
A: Use the pre-incement/decrement form (++i and --i)
Even though there is little impact for fundamental types, since we programmers are human, you will produce programming habits. And those habits will cary over to when we deal with classes instead, for example iterators.
There was an informal study done (sorry I forgot the source) on a number of people who say "I only use post-incrementing/decrementing for fundamental types." The results showed that they very frequently used the post-increment/decrementing operators for iterators and other classes leading to a great amount of wasted CPU cycles in the end code.
Last edited by Andreas Masur; July 24th, 2005 at 04:38 AM.
-
February 5th, 2007, 04:33 AM
#3
Re: C++ Operator: Why should I use '++i' instead of 'i++'?
There is very little gain over efficiency for fundamental types and efficiency as a concern for them doesn't help you decide which to choose. The underlying idea should be in your mind. The post-increment causes a copy to be made, which becomes useless. Such style should be avoided.
Moreover, there can be situations where you need to use the post-increment rather than pre-increment. For example, when removing an element from a standard map container object.
Code:
mapObject.erase(aValidIterator++);
Of course, you can use a copy explicitly and use pre-increment but this erase functionality looks more elegant as a one-liner.
Also, take a look at the following - Which is faster, i++ or ++i?
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++?
-
May 12th, 2007, 01:49 PM
#4
Re: C++ Operator: Why should I use '++i' instead of 'i++'?
There is very little gain over efficiency for fundamental types and efficiency as a concern for them doesn't help you decide which to choose.
There should have been ..."in recent years (about 20)"...
The pre-increment was specifically designed for "C" on the DEC PDP-11 (remember "C" has only fundamental types), because there was a specific hardware instruction that performed this operation directly on the mrmory location, without the overhead of a load,add,store sequence. The result was significantly faster.
There are still a few processors that can do this more efficiently (than the post increment of a fundamental type), but processors are often 1000's of times faster today and the situations are extremely rare. The only place I can (possibly) recomment the use of a post increment is:
Code:
int j = i++; // Save the pre-incremented value for later calculations.
This is (at least to me) more readable and maintainable [and is of higher efficiency than:
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
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
|