mrwad99
asked on
Ternary operator: odd behaviour when combined with operator +
Ah hello.
I put the following code in a program I am working on and was surprised at the result:
CString s ( _T("This is a long string") );
int n = 1 + ( s.GetLength() ) ? 1 : 0;
n is 1; why is it not 2?
Interestingly,
int n = 1 + ( s.GetLength() > 0 ) ? 0 : 1;
results in n being 0.
I might be having a moment of madness here, but what is going on??
TIA
I put the following code in a program I am working on and was surprised at the result:
CString s ( _T("This is a long string") );
int n = 1 + ( s.GetLength() ) ? 1 : 0;
n is 1; why is it not 2?
Interestingly,
int n = 1 + ( s.GetLength() > 0 ) ? 0 : 1;
results in n being 0.
I might be having a moment of madness here, but what is going on??
TIA
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks :)