Public Group

#### Archived

This topic is now archived and is closed to further replies.

# C++: ? operator

This topic is 5351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I tried looking this up in Google and my C++ manual...but it''s not so easy to find with a search, obviously ...how does the ''?'' operator work?

##### Share on other sites
The ternary operator is a (the) conditional expression.
condition ? doiftrue : doiffalse

e.g.
int x = (a == 2)? 3 : 5;

[edited by - Miserable on February 22, 2004 11:49:01 PM]

##### Share on other sites
It''s more of an alternative to using a if/else block for example (using the example above)
if(a == 2){    x = 3;}else{    x = 5}

as you can see the ?: operator is a lot more concise an also allows you to achieve the equivalent of an if/else block on return statements:

bool SomeFunc(void){     int *Ptr = NULL;    //blah   return ((Ptr == NULL) ? false, true);}

##### Share on other sites
quote:
Original post by Spudder
return ((Ptr == NULL) ? false, true);

That''s the perfect example on how to NOT use the ternary operator. This line should be:
return( Ptr != NULL );

The ternary operator is usually used as a shortcut to an if/else or switch block, if an assignment is based on a selection. For example:
*( isBoxFilled ? DrawFilledBox : DrawEmptyBox )(Coords, Color);

if( isBoxFilled )   DrawFilledBox(Coords, Color);else   DrawEmptyBox(Coords, Color);

Or perhaps:
color buttonColor = ( alert > 0 ) ? colorRed : colorGray;

##### Share on other sites
I wasn''t saying that was the best way to use the ?: operator, but it was the first example i could think of which demonstrated how ?: could represent an if/else block in a situation when you can''t actually use one which hopefully explained some of the benefits as when i was first starting out with C++ i couldn''t quite see the advantage of ?:

##### Share on other sites
quote:
Original post by Spudder
I wasn''t saying that was the best way to use the ?: operator, but it was the first example i could think of which demonstrated how ?: could represent an if/else block in a situation when you can''t actually use one which hopefully explained some of the benefits as when i was first starting out with C++ i couldn''t quite see the advantage of ?:

Well, the problem was not so much the use of ?: in itself, but the fact that your :? returned a boolean value. A ?: returning a bool simply doesn''t make sense, as the input of ?: is already a boolean.

These two:
bool b = a ? true : false;bool c = a ? false : true;

are totally equal to
bool b = a;bool c = !a;

A statement such as
return ((Ptr == NULL) ? false : true);

is valid C/C++, and will work, but it is redundant and meaningless. The ?: basically becomes a NO-OP, since the result is already available in the condition itself. Just as, for example
if( v == 2 ) v = 2;

So generally speaking, returning a bool from a ?: works fine, but it''s bad style. It won''t give any additional functionality, but make your code more difficult to read, and obfuscated.

##### Share on other sites
A good place to use the ternary operator is in an intializer list of a class constructor:

class Happy{    Happy(int i, int j) : m_happy(i>5?5:i), m_joy(j<10?10:j)    {}    //...};

--{You fight like a dairy farmer!}

##### Share on other sites
quote:
Original post by ALX
Well, the problem was not so much the use of ?: in itself, but the fact that your :? returned a boolean value. A ?: returning a bool simply doesn''t make sense, as the input of ?: is already a boolean.

Plus, he used a comma (,) instead of a colon (...it shouldn''t even compile

Regards,
Jeff

1. 1
Rutin
25
2. 2
3. 3
4. 4
5. 5

• 9
• 10
• 13
• 19
• 14
• ### Forum Statistics

• Total Topics
632945
• Total Posts
3009359
• ### Who's Online (See full list)

There are no registered users currently online

×