How will you best differentiate the two?
i++; //and
++i;
How will you best differentiate the two?
i++; //and
++i;
++i increments i and then uses the new value, while i++ uses the old value and then increments i.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
eg.
Code:int i = 10; cout << i-- << endl; //prints 10 (returned THEN decremented) cout << --i << endl; //prints 8 (decremented THEN returned)
Code:int i = 0; //first example, b will be 1 int b = ++i; //second example, b will be 0 int b = i++;
Note also that in C++, when you are using --x or ++x for your own class (e.g. an iterator), then the calculation is simpler than for x++ or x--, because the latter requires a temporary copy of the value to be returned, and this adds extra work for the compiler. This has been discussed quite a lot - many C++ programmers therefor prefer to use --x or ++x whenever possible, even on standard base-types that aren't subject to custom operator functions [and thus have no difference in performance].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Actually, it will print 8, because just before that point i=9.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Uh? For starters you are worng. It prints 8. Next, did you really have a question or are you wasting our time. You seem to know the answer.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.