Link to home
Create AccountLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

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
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mrwad99

ASKER

Excellent.  I see now.

Thanks :)