R
Richard Tierney
I'd like to do something like this:
cout << (A? x : B? y:z) << endl;
where x, y, and z are signed and unsigned versions of the same type.
However, this doesn't work. When the expression in brackets is
evaluated, it does something more complicated than simply returning
one of x, y, or z. Can someone tell me what the line above actually
does? Complete (short) test program below.
Thanks -
Richard
----------------------------------------------------------
#include <iostream>
#include <iomanip>
using std::dec;
using std::cout;
using std::endl;
main() {
bool A = false;
bool B = true;
unsigned x = 10;
int y = -20;
int z = 30;
cout << (A? x : B? y:z) << endl; // this prints '4294967276'..
if(A)
cout << dec << x << endl;
else if(B)
cout << dec << y << endl; // but this prints '-20'!
else
cout << dec << z << endl;
}
cout << (A? x : B? y:z) << endl;
where x, y, and z are signed and unsigned versions of the same type.
However, this doesn't work. When the expression in brackets is
evaluated, it does something more complicated than simply returning
one of x, y, or z. Can someone tell me what the line above actually
does? Complete (short) test program below.
Thanks -
Richard
----------------------------------------------------------
#include <iostream>
#include <iomanip>
using std::dec;
using std::cout;
using std::endl;
main() {
bool A = false;
bool B = true;
unsigned x = 10;
int y = -20;
int z = 30;
cout << (A? x : B? y:z) << endl; // this prints '4294967276'..
if(A)
cout << dec << x << endl;
else if(B)
cout << dec << y << endl; // but this prints '-20'!
else
cout << dec << z << endl;
}