Hi Dan,
Here is the other code. You'll see it is completely identical!!! but it produces the output "first second third".
I woke up at 4 am, and understood why both answers are possible (but not quite why my vc++ chooses between them randomly).
Consider this simpler program:
char* order(){
//<s>cout << "second ";</s>
return "third ";
}
void main(){
cout<<"first "<<order();
}
As always, the things in cout must be
printed in left to right order, so it must be "first third".
But there is no rule about the order that they are
calculated. So the program can calculate the return value of the function before or after figuring that "first " is a text string.
But it must print them in the correct order.
Unfortunately, the actual function that I used has an extra cout in the body. This will be executed when the calculations are done. And as I've said either order of calculation is legal.
...And the correct way of saying that my function has something going on in the body of it is "the function has a side effect"
I must credit two guys on usenet for helping me with this...
I didn't understand what they were saying, but my subconscious must've figured it out!
http://groups.google.co.uk/group/mic...3333e8712b5bb5
Here's my other code. You'll see it's no different
:
#include <iostream>
using namespace std;
char* order(){
cout << "second ";
return "third";
}
void main(){
cout<<"first "<<order();
}
I'll email you.
Tony