We help IT Professionals succeed at work.

comparison operation

DJ_AM_Juicebox
on
Hi,

In c++, I can do this:

     int n = 5;
     n == 0 ? ok() : notok();

This doesn't seem to be possible, keep getting compilation errors about the n == 0 part, can't convert integer to boolean?

Is there some way to do that?

Thanks
Comment
Watch Question

Commented:
ok() or notok() should return a value...

like..

var = (n==0) ? ok() : notok();

Author

Commented:
musn't both return a value then? I mean what would the effect of var = (void SomeFunc()) be?

Also, why do they HAVE to return a value? What if the two functions are just meant to return void?

Thanks
Commented:
>> what would the effect of var = (void SomeFunc()) be?

You cannot use function returning void in ?: operator..

You must use function returning some value...

Commented:
As every operator (+, -) is expected to return a value..

?: (Conditional operator) is also expected to return a value

Explore More ContentExplore courses, solutions, and other research materials related to this topic.