Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > side effects

Reply
Thread Tools

side effects

 
 
andrew browning
Guest
Posts: n/a
 
      05-14-2006
does i++ have the same potential for side effects as its pre-fix
equivilent ++i? and if so, is it always dangerous to use them, as in a
for loop, or only in certain circumstances?

 
Reply With Quote
 
 
 
 
Alan Johnson
Guest
Posts: n/a
 
      05-14-2006
andrew browning wrote:
> does i++ have the same potential for side effects as its pre-fix
> equivilent ++i? and if so, is it always dangerous to use them, as in a
> for loop, or only in certain circumstances?
>


Define "potential for side effects". Both ++i and i++ will increment i,
if that is what you are asking (or in the case of class types, call the
corresponding version of operator++).

Also, specify what you mean by "dangerous to use them". It is quite
common for increment operators to be used in for loops:
for (unsigned i = 0; i < 10; ++i) { // Do something }

The only dangerous use I can think of is trying to use an increment
operator in an expression where the operand is also being used for
something else:
i = ++i + 1; // unspecified behavior
j = i + i++; // unspecified behavior
j = i++ + ++i; // unspecified behavior

Also, since it inevitably will come up:
http://www.parashift.com/c++-faq-lit...html#faq-13.15

--
Alan Johnson
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-14-2006
* andrew browning:
> does i++ have the same potential for side effects as its pre-fix
> equivilent ++i?


No, i++ is more side effect oriented than ++i because i++ has only one
reason for existence: producing a side effect in an expression.

i++ can be regarded as (temp = i, ++i, temp).


> and if so, is it always dangerous to use them


No.


> as in a for loop, or only in certain circumstances?


No. But prefer ++i. That way you tell the reader that you're not
interested in the side effect, and it can be more efficient.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
andrew browning
Guest
Posts: n/a
 
      05-14-2006
got it.

am i too take it that i'm breaking usenet protocol somehow? i hope i
am not

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-14-2006
* andrew browning:
> got it.
>
> am i too take it that i'm breaking usenet protocol somehow? i hope i
> am not


Well, now you are, because you forgot to quote what you responded to.
But originally you were not. Heh.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Url Rewriting with just an HttpHandler (without the side-effects) Anonieko ASP .Net 0 08-12-2005 01:10 AM
Calling DataBind() multiple times-- strange side effects? Jim Bancroft ASP .Net 2 12-28-2004 12:57 PM
Evaluating expressions with side effects. Jason Heyes C++ 10 12-21-2004 05:17 PM
Object reference side effects - desirable? VisionSet Java 7 11-09-2004 02:09 AM



Advertisments