🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by douglas jackson Sun, 18 Oct 1998 00:00
Pretty odd
#119
Author: douglas jackson
Date: Sun, 18 Oct 1998 00:00
30 lines
975 bytes
If I push the numbers 1, 2, 3, 4, and 5, onto a stack called Stck1, in that
order (i.e. 5 is on the top of the stack), and then I print 
them using Pop and Top, I get some weird results.  If I loop
over this print statement:

cout <<Stck1.Top_STK()<<Stck1.Pop_STK()<<endl;

I get

45
34
23
12
331

with the "33" being a trash number.  If I loop over this statement:

cout <<Stck1.Top_STK();
cout <<Stck1.Pop_STK()<<endl;

I get

55
44
33
22
11

which is what you would expect.  Is there some sort of order of
operations in a cout statement with classes?  
Re: Pretty odd
#120
Author: ken shrum
Date: Mon, 19 Oct 1998 00:00
20 lines
1190 bytes
douglas jackson (jacksond@alain.cs.colostate.edu) wrote:
[ this statement ]
: cout <<Stck1.Top_STK()<<Stck1.Pop_STK()<<endl;
[ and these statements ]
: cout <<Stck1.Top_STK();
: cout <<Stck1.Pop_STK()<<endl;
[ give different results ]
: Is there some sort of order of
: operations in a cout statement with classes?  

The technical term here is "sequence point" - the first statement
is a single expression, and it contains expressions with side-effects
(the "Top_STK" and "Pop_STK" calls).  Consequently, the Top and Pop
are evaluated in some undefined order before operator<< is invoked
to write to cout.

Your latter structure is more correct, or you can use temporary
variables to make the order of computation explicit.

    Ken Shrum
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads