1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
using namespace std;
int a = 0;
int main()
{
if ((a) == (a = 1))
{
if (a)
{
cout << "Failure.\n\na = " << a << ".\n" << endl;
}
else cout << "Complete failure.\n\na = " << a << ".\n" << endl;
}
else
{
if (!a)
{
cout << "Partial failure.\n\na = " << a << ".\n" << endl;
}
else
{
cout << "Success.\n\na = " << a << ".\n" << endl;
}
}
return 0;
}
|
What I'm trying to do one line 8 is, take the value that is stored in A, hold on to it, put a new value in A, and then compare the new value of A with the old value. I guess what I'm asking is are boolean expressions passed, or evaluated by value or by reference?
i.e. when the compiler reads a =, does it compare the value that is stored in a at that moment, or does it or does it use the address?
What it appears is it passes the address, so that assignment within the if expression like I have tried will fail to compare the initial value, which is what I want.
I'd like to be able to use the comma operator, so something like this:
but it dawned on me that here the comma operator is ignoring everything up to the final a, which is not what I want. I want the comma operator to ignore just the asssignment so that what I get is something akin to:
where A1 is the initial value of a and A2 is the final value of a, but without declaring new variables.
Thanks for your help.