943,833 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1606
  • C++ RSS
Apr 10th, 2007
0

C++ help

Expand Post »
  1. cout << "\nYou trade your sword for a battle axe.";
  2. inventory[0] = "battle axe";
  3. cout << "\nYour items:\n";
  4. for (int i = 0; i < numItems; ++i)
  5. cout << inventory[i] << endl;
Uhh well the question is that I really dont get the ++i Why would you use the pre fix instead of the post fix, I know how they work its just doesnt really make sense
Last edited by WaltP; Apr 11th, 2007 at 3:47 am. Reason: Added CODE tags -- you actually typed right over what they are when you entered this post...
Reputation Points: 9
Solved Threads: 0
Junior Poster
Tauren is offline Offline
108 posts
since Oct 2006
Apr 10th, 2007
0

Re: C++ help

first, please post with code tags...


second, it's really use choice... it just increments the variable 'i' before it's accessed. may as well stick to "i++" imo.
Reputation Points: 11
Solved Threads: 17
Junior Poster
mariocatch is offline Offline
103 posts
since Apr 2007
Apr 10th, 2007
0

Re: C++ help

Some people have argued that ++i yields more efficient assembly code. When I checked, they were the same. In this situation, it doesn't matter.
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Apr 10th, 2007
0

Re: C++ help

Ur right, If you had a server and maybe the data base which holds the items you wouldnt want the server holding the item before you got it I guess I will always just use i++
Reputation Points: 9
Solved Threads: 0
Junior Poster
Tauren is offline Offline
108 posts
since Oct 2006
Apr 10th, 2007
0

Re: C++ help

I just dont fer what the statments do? whats the diffrence?

/code for (int i = 0; i < numItems; ++i)
what does that do like? lol explain
Reputation Points: 9
Solved Threads: 0
Junior Poster
Tauren is offline Offline
108 posts
since Oct 2006
Apr 10th, 2007
0

Re: C++ help

i++ Would be increased before code is done
++i is increased before code is done
Reputation Points: 9
Solved Threads: 0
Junior Poster
Tauren is offline Offline
108 posts
since Oct 2006
Apr 10th, 2007
0

Re: C++ help

Click to Expand / Collapse  Quote originally posted by Infarction ...
Some people have argued that ++i yields more efficient assembly code. When I checked, they were the same. In this situation, it doesn't matter.
in this specific case it really does not matter. When applied to primitives such
as int or char, they are indistinguishable in terms of efficiency. When applied
to objects of a user defined type the difference can be significant; the prefix
version avoids the creation of an anonymous temporary.

try this code:
C++ Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. struct A
  4. {
  5. A( int ii ) : i(ii) { cout << "A::constructor\n" ; }
  6. A(const A& that ) : i(that.i) { cout << "A::copy_constructor\n" ; }
  7. ~A() { cout << "A::destructor\n" ; }
  8. A& operator++() // prefix
  9. { ++i ; return *this ; }
  10. A operator++(int) // postfix
  11. { A temp(*this); ++i ; return temp ; }
  12. operator int() const { return i ; }
  13. int i ;
  14. };
  15. cout << "------------- start -----------------------------\n" ;
  16. A a1 = 100, a2 = 100 ;
  17. cout << "a1: " << a1 << "\ta2: " << a2 << '\n' ;
  18. cout << "------------- prefix++ ------------------------\n" ;
  19. int i = ++a1 ;
  20. cout << "a1: " << a1 << "\ti: " << i << '\n' ;
  21. cout << "------------- postfix++ -----------------------\n" ;
  22. i = a2++ ;
  23. cout << "a2: " << a2 << "\ti: " << i << '\n' ;
  24. cout << "------------- done -----------------------------\n" ;
  25. };
and here is the output:
------------- start -----------------------------
A::constructor
A::constructor
a1: 100 a2: 100
------------- prefix++ ------------------------
a1: 101 i: 101
------------- postfix++ -----------------------
A::copy_constructor
A::destructor
a2: 101 i: 100
------------- done -----------------------------
A::destructor
A::destructor


we can really see the difference now between ++a1 and a2++, both
in terms of the difference in semantics as well as performance.

if you are into a lot of C++ programming, it is a good idea to get
into the habit of using the prefix versions of ++ and -- as the normal
case. even for builtin types like int, the prefix version is as efficient
as the postfix version.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Apr 11th, 2007
0

Re: C++ help

Click to Expand / Collapse  Quote originally posted by Tauren ...
Ur right, If you had a server and maybe the data base which holds the items you wouldnt want the server holding the item before you got it I guess I will always just use i++
Click to Expand / Collapse  Quote originally posted by Tauren ...
I just dont fer what the statments do? whats the diffrence?

/code for (int i = 0; i < numItems; ++i)
what does that do like? lol explain
Huh? Try using the PREVIEW button and proofreading your posts please. And please read this, too.


Try this program for illustration:
  1. int main()
  2. {
  3. int i = 5;
  4.  
  5. cout << " i = " << i << endl;
  6. cout << "prefix i = " << ++i << endl;
  7. cout << "now i = " << i << endl;
  8. cout << "postfix i = " << i++ << endl;
  9. cout << "now i = " << i << endl;
  10. return 0;
  11. }
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Prime_NUMBER
Next Thread in C++ Forum Timeline: urgent help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC