Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Unpredictab...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 12 Topic 45818 of 48020
Post > Topic >>

Re: Unpredictable nature of increment operators

by "Victor Bazarov" <v.Abazarov@[EMAIL PROTECTED] > May 9, 2008 at 01:21 PM

Martin York wrote:
> On May 8, 5:16 pm, bintom <binoythomas1...@[EMAIL PROTECTED]
> wrote:
>> Why doe the following C++ code behaves as it does?
>>
>> int i;
>>
>> i=1;            cout << (++i)++;          Output: 2
>> i=1;            cout << ++(++i);          Output: 3
>> i=1;            cout << (i++)++;          Output: Error. LValue
>> required i=1;            cout << ++(i++);          Output: Error.
>> LValue required
>
> Because code like that is so hard to read you would never actually
> write it!
>
> Also remember that it is illegal to modify a variable more than once
> within the same expression [undefined behavior I believe](somebody
> with a copy of the standard will be able to quote you chapter an verse
> and the actual definition rather than my layman's wording).

Only a variable of a built-in type, and only if there is no sequence
point between the modifications.  For example, it's not undefined
behaviour to do

    f(i++) + ++i;

IIRC, but the results are unspecified.  And if your class has the
operators ++() or ++(int) overloaded, the results are even well-
defined and predictable.  For 'int' they aren't...

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 




 12 Posts in Topic:
Unpredictable nature of increment operators
bintom <binoythomas110  2008-05-08 17:16:56 
Re: Unpredictable nature of increment operators
Ian Collins <ian-news@  2008-05-09 12:17:58 
Re: Unpredictable nature of increment operators
Martin York <Martin.Yo  2008-05-09 10:11:08 
Re: Unpredictable nature of increment operators
"Victor Bazarov"  2008-05-09 13:21:26 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 08:47:59 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-10 07:08:42 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 09:01:10 
Re: Unpredictable nature of increment operators
"Bo Persson" &l  2008-05-11 10:56:59 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-11 09:05:09 
Re: Unpredictable nature of increment operators
Old Wolf <oldwolf@[EMA  2008-05-11 02:14:44 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:09:34 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:19:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:49:07 CDT 2008.